I may be barking up the wrong tree e-mailing an OpenJDK list when I've
only reproduced this against an Oracle JDK (1.7.0_25, and in particular
it doesn't reproduce against 1.7.0_21), but the attached file produces
NPEs [1] via EventQueue.isDispatchThread() fairly regularly with an
argument of 10 [threads] and can even produce it with just 2 (although
less often).

We've had even weirder NPEs [2] in the code behind that but I have yet
to distill them to a repro unfortunately.

Are either of these known?  Is there something we should be doing in our
rather multi-threaded environment to protect this code?

Keith

[1] E.g.:

> java.lang.NullPointerException
>       at java.awt.EventQueue.isDispatchThread(EventQueue.java:1014)
>       at EQNPE$1.run(EQNPE.java:23)

[2] Ending in:

> java.lang.NullPointerException
>       at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunToolkit.java:1011)
>       at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunToolkit.java:1007)
>       at 
> sun.awt.HeadlessToolkit.getSystemEventQueueImpl(HeadlessToolkit.java:352)
>       at java.awt.Toolkit.getSystemEventQueue(Toolkit.java:1717)
>       <our code>
import java.awt.EventQueue;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;

public class EQNPE {
    public static void main(String[] args) throws InterruptedException {
        int N_THREADS = Integer.parseInt(args[0]);
        final CountDownLatch started = new CountDownLatch(N_THREADS);
        final CountDownLatch start = new CountDownLatch(1);
        LinkedList<Thread> ts = new LinkedList<Thread>();
        for(int i = 0; i < N_THREADS; ++i) {
            final int iFinal = i;
            Thread t = new Thread("thread-" + i) {
                @Override
                public void run() {
                    started.countDown();
                    try {
                        start.await();
                    }
                    catch(InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    EventQueue.isDispatchThread();
                }
            };
            t.start();
            ts.add(t);
        }
        started.await();
        start.countDown();
        for(Thread t : ts) {
            t.join();
        }
    }
}

Reply via email to