Reputation: 105133
This is my code:
// in constructor
BlockingQueue<Node> queue = new LinkedBlockingQueue<Node>();
// later in another thread
Node node = queue.poll(1, TimeUnit.SECONDS);
Usually it works, but sometimes, under some circumstances (still don't know when and why) poll()
method doesn't return NULL
but keeps its thread in WAITING
state forever. Why and how this could happen?
I tried ArrayBlockingQueue
- the same effect. I'm using OpenJDK on Mac OS:
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
The same code works just fine with Oracle Java 1.6 on Mac OS. This is where the threads are stuck:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:894)
java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1221)
java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:340)
java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:462)
What's interesting is that when I interrupt()
this thread and try to poll()
again I reach the same situation immediately.
Upvotes: 20
Views: 3596