I am looking for a way of pausing a Thread.
I started with affectively using a boolean flag (called 'paused'), and wrapping a check with a while loop (pause).
Within the while loop there’s a Thread.wait()
to block the execution.
I’ve been looking at the AtomicBoolean, which seems to do the trick apart from it doesn’t block.
Is there a alternative or extended version of AtomicBoolean that has a block method ?
i.e. something like AtomicBoolean.getFalse()
of AtomoicBoolean.get(false)
?
They have a Blocking Queue, so a Blocking value.
Current setup is :
while (paused.get()) {
synchronized (paused) {
try {
paused.wait();
} catch (Exception e) {
}
paused.notify();
}
}
with
public void pause() {
if (paused.compareAndSet(false, true)) {
synchronized (paused) {
paused.notify();
}
}
}
public void resume() {
if (paused.compareAndSet(true, false)) {
synchronized (paused) {
paused.notify();
}
}
}