wf.utils.java.thread.mutex.Mutex Maven / Gradle / Ivy
The newest version!
package wf.utils.java.thread.mutex;
import java.util.concurrent.atomic.AtomicBoolean;
public class Mutex {
private final AtomicBoolean lock;
private final Object mutex;
public Mutex() {
this.lock = new AtomicBoolean(false);
this.mutex = new Object();
}
public Mutex(boolean lock) {
this.lock = new AtomicBoolean(lock);
this.mutex = new Object();
}
// while(!thread.isInterrupted()){
// mutex.step();
// ...
// }
public void step() {
if (lock.get()) synchronized(mutex) {
try {
mutex.wait();
} catch (InterruptedException ignored) {}
}
}
public void lock() {
lock.set(true);
}
public void unlock() {
lock.set(false);
synchronized(mutex) {
mutex.notify();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy