Question

Java's Executor is (as far as I understand it) an abstraction over the ThreadPool concept - something that can accept and carry out (execute) tasks.

I'm looking for a similar exception for the Polling concept. I need to continuously poll (dequeue) items out of a specific Queue (which does not implement BlockingQueue), execute them and sleep, and repeat all this until shutdown.

Is there a ready-made abstraction or should I write something on my own?

(Suggestions of a better title are welcome)

Was it helpful?

Solution

Polling is easy:

Thread t = new Thread(new Runnable() {
    public void run() {
        try {
            while (!t.isInterrupted()) {
               Object item;
               while ((item = queue.take()) == null) {//does not block
                   synchronized (lock) { lock.wait(1000L) } //spin on a lock
               }
               //item is not null
               handle(item);
            }
        } catch (InterruptedException e) { }
    }
});
t.start();

Perhaps you need to rephrase your question as I'm not quite sure exactly what it is you are trying to do?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top