yegor256
yegor256

Reputation: 105133

Why LinkedBlockingQueue#poll() may hang up?

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

Answers (1)

Unai Vivi
Unai Vivi

Reputation: 3101

That's a [nasty] issue that has been solved in the early versions of Java 7. If you migrate to the newer JVM you won't have that problem (it appears the fix has not been back-ported to Java 6).

See here and here.

Upvotes: 3

Related Questions