I use an ArrayBlockingQueue
in my code. Clients will wait until an element becomes available:
myBlockingQueue.take();
How can I "shutdown" my service in case no elements are present in the queue and the take()
is waiting indefinitely 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 doesn't 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
?