[concurrency-interest] Strange behavior with LinkedBlockingQueue

David Holmes dholmes@dltech.com.au
Wed, 27 Oct 2004 10:07:51 +1000


This is a bug in LinkedBlockingQueue.drainTo, which forgets to reset the
tail pointer to indicate an empty queue. A bug report will be submitted -
thanks!

Here's a simpler test case to demonstrate that drainTo only works once:

        BlockingQueue<Object> q = new LinkedBlockingQueue<Object>();
        int count = 0;
        ArrayList<Object> l = new ArrayList<Object>(1000);
        while(true) {
            for (int i = 0; i < 10000; i++)
                q.put(NULL_OBJECT);
            System.out.println("Produced 10000");
            count = q.drainTo(l);
            for (int i = 0; i < count; i++) {
                l.get(i);
            }
            System.out.println("count = " + count);
            if (count > 0)
                l.clear();
        }

Produces:

Produced 10000
count = 10000
Produced 10000
count = 0
Produced 10000
count = 0
Produced 10000
count = 0
...

David Holmes