0

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?

IgalS
  • 31
  • 3
  • Just to be clear, your thread should be woken up when there's an event in the queue **and** some condition is true? – Sotirios Delimanolis Jan 09 '14 at 21:05
  • Maybe some type of Semaphore? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html Also http://stackoverflow.com/questions/4046838/is-there-any-unbounded-fair-blocking-queue-in-java – Mark W Jan 09 '14 at 21:41
  • Can it be that you mixed the task manager and event manager parts up in your question? It's not so clear what you mean. – fatih Jan 09 '14 at 22:53
  • Yes, my thread should be woken up when there's an event in the queue and some condition is true. i've edited the question hope that's more clear now.. – IgalS Jan 10 '14 at 07:17

0 Answers0