But it doesn't catch the interrupt if it was interrupted just before calling that method.
So if I'm understanding, your question, then something is swallowing an InterruptedException
or clearing the interrupt flag. The following code always throws InterruptedException
for me.
Thread.currentThread().interrupt();
new LinkedBlockingQueue<String>().take();
It is important to realize that when the InterruptException
is thrown, the interrupt flag is cleared. You should always do something like:
try {
// other methods where this is needed are Object.wait(...), Thread.join(...)
Thread.sleep(100);
} catch (InterruptedException ie) {
// re-interrupt the thread
Thread.currentThread().interrupt();
// deal with the interrupt by returning or something
...
}
See: Why invoke Thread.currentThread.interrupt() when catch any InterruptException?
What often happens is that 3rd party code does not propagate the interrupt status because of bad code. Then you are often SOL since the interrupt will have been swallowed and the take()
method will not throw.
Also, it is important to realize that Thread.interrupted()
clears the interrupt flag. Typically you want to use Thread.currentThread().isInterrupted()
to test for the status of the interrupt flag.
Is there an alternative BlockingQueue implementation addresses this issue?
I'm not sure there is an issue.