I've two threads the first one execute some tasks (called TaskManager) and the second listen to events and store them in a queue (called EventManager). EventManager should be woken up and start running only if the queue is not empty and some condition is true.(when EventManager is not currently executing !eventManager.isRunning())
Ex. code:
class TaskManager implements Runnable {
@Override
public void run() {
try {
while (true) {
Object event = blockingQueue.take();
while(event != null && eventManager.isRunning()) {
}
// handle event
}
} catch (InterruptedException ie) {
// handle exception
} catch (Exception e) {
// handle exception
}
}
}
In this way the thread will be waiting as long as nothing is in the queue but will loop endlessly if the eventManager is still running which will cause starvation.
Is there any existing framework like blockingQueue that will wait and be woken when something is inserted to the queue and another condition will meet(which keep Fairness).
I can solve that by using notify() & wait() myself but i prefer to use existing solutions.
Any suggestions will be appreciated?