ArrayBlockingQueue - How to "interrupt" a thread that is wating on .take() method

Posted by bernhard on Stack Overflow See other posts from Stack Overflow or by bernhard
Published on 2010-06-05T03:41:32Z Indexed on 2010/06/05 8:22 UTC
Read the original article Hit count: 278

Filed under:
|

I use an ArrayBlockingQueue in my code. Clients will wait untill an element becomes available:

myBlockingQueue.take();

How can I "shutdown" my service in case no elements are present in the queue and the take() ist wating indefenitely for an element to become available? This method throws an InterruptedException. My question is, how can I "evoke" an Interrupted Exception so that take() will quit? (I also tought about notify(), but it seems I doesnt help here..)

I know I could insert an special "EOF/QUIT" marker Element but is this really the only solution?

UPDATE (regarding the comment, that points to another question with two solutions: one mentioned above using a "Poisoning Pill Object" and the second one is Thread.interrupt():

The myBlockingQueue.take() is used NOT in a Thread (extending Thread) but rather implements Runnable. It seems a Runnable does not provide the .interrupt() method? How could I interrupt the Runnable?

Million Thanks

Bernhard

© Stack Overflow or respective owner

Related posts about java

Related posts about concurrency