JDK-6450705 : NullPointerException in LinkedBlockingQueue.peek
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-07-19
  • Updated: 2010-04-02
  • Resolved: 2006-10-23
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP Pro sp2

EXTRA RELEVANT SYSTEM CONFIGURATION :
multiple threads

A DESCRIPTION OF THE PROBLEM :
Caused by: java.lang.NullPointerException
            at java.util.concurrent.LinkedBlockingQueue.peek(LinkedBlockingQueue.java:440)

this function is

    public E peek() {
        if (count.get() == 0)
            return null;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            Node<E> first = head.next;
            if (first == null)
                return null;
            else
                return first.item;
        } finally {
            takeLock.unlock();
        }
    }

line 440 is  Node<E> first = head.next;

i think that you need to add in line 440

        if (count.get() == 0)
            return null;

a second time. the difference this time is that you are in a protected section enforced by takeLock.lock();

thanks.
 


REPRODUCIBILITY :
This bug can be reproduced occasionally.

Comments
EVALUATION I can't find anywhere in the source where head is assigned null, so the given code apparently cannot throw NullPointerException. We need a reproducible test case. Here's a start (which does not succeed in reproducing any problem): -------------------- import java.util.*; import java.util.concurrent.*; public class Bug { public static void main(String[] args) throws Throwable { final int iterations = 50000000; final BlockingQueue<Integer> q = new LinkedBlockingQueue<Integer>(); final Thread t = new Thread() { public void run() { for (int i = 0; i < iterations; i++) { q.add(1); q.poll(); }}}; t.start(); for (int i = 0; i < iterations; i++) q.peek(); } }
19-07-2006