I have a simple "play/pause" flag. Imagine a doer thread:
private final Thread backgroundDoer = new Thread(() -> {
while(true) {
doOperation();
playFlag.await();
}
})
So keep doing an operation unless the flag is false, in which case wait for the flag to be true to continue.
My question is how to do this properly in Java. I can't really use a Condition
because I would need to keep signalling it from outside. Right now I am using a Semaphore
. When "play" is pressed I give it a lot of permits, when pause is pressed I drain them all away. It seems clunky and stupid though. Any better ways?