[concurrency-interest] LinkedBlockingQueue.clear() broken?

Jasper Potts jasper@jasperpotts.com
Wed, 01 Dec 2004 19:02:12 +0000


Heres a simple example

LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
queue.add("A");
queue.add("B");
System.out.println("poll() = " + queue.poll());
queue.clear();
queue.add("C");
System.out.println("poll() = " + queue.poll());

I get :

poll() = A
Exception in thread "main" java.lang.NullPointerException
    at 
java.util.concurrent.LinkedBlockingQueue.extract(LinkedBlockingQueue.java:139)
    at 
java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:420)

but would have expected:

poll() = A
poll() = C

Looking at the source it looks like the clear method changes the "head" 
but not the "last" for the linked list. So "last" is left pointing at 
the old node. "add()" or "offer()" append to the "last" not touching the 
"head". So the queue thinks it has elements but none are connected to 
the head.

Am I being stupid or should I report this as a bug to Sun?

Many Thanks

Jasper Potts