[concurrency-interest] Queue with interrupt

Jochen Theodorou blackdrag at gmx.org
Wed May 17 02:15:05 EDT 2017

Hi all,

yesterday I was looking for a certain behaviour, but was not finding one 
and I am wondering if I did overlook something.


I have 1 Thread - lets call it worker, that needs to execute a series of 
tasks. If there are no tasks this thread has to wait for a maximum of 5 
seconds. After this time I have to send something like an update from 
this thread (has to be the same thread) to later continue on waiting for 
tasks or handle the new ones. At this point I will not car if processing 
the tasks takes longer than 5 seconds. And now the specialty: A second 
thread must be able to send an interrupt to the worker thread, which 
than has to stop waiting for new jobs and send the update right away, to 
then get back to waiting for jobs. I cannot send the update from the 
interrupting thread.

Now I was expecting to find something in a Queue implementation, but 
while I did find a way to wait for new elements, I did not find a wait 
to interrupt that waiting. We solved this by using a BlockingQueue, plus 
some signaling, but it really does not feel right to me.

Any idea how to do this better?

bye Jochen

More information about the Concurrency-interest mailing list