Brecht Yperman
Brecht Yperman

Reputation: 1279

Infinite loop in EventQueue.isDispatchThread()

I have a Java program taking 100% cpu, but seemingly doing nothing.

If I take a thread dump, there are 4 threads (out of a pool of 5) waiting to take a lock.

"Incoming WorkPool 5" - Thread t@363
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - waiting to lock <7212149b> (a java.util.concurrent.locks.ReentrantLock$NonfairSync) owned by "Incoming WorkPool 3" t@354
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:867)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1197)
    at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:214)
    at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:290)
    at java.awt.EventQueue.isDispatchThreadImpl(EventQueue.java:1019)
    at java.awt.EventQueue.isDispatchThread(EventQueue.java:1014)

The thread they are waiting for is RUNNABLE

"Incoming WorkPool 3" - Thread t@354
   java.lang.Thread.State: RUNNABLE
    at java.awt.EventQueue.isDispatchThreadImpl(EventQueue.java:1024)
    at java.awt.EventQueue.isDispatchThread(EventQueue.java:1014)

This is JDK 7.0.25, so it seems one thread is stuck on

EventQueue next = eq.nextQueue;
while (next != null) {
    eq = next;
    next = eq.nextQueue;
}

There are two AWT EventQueue threads, trying to acquire the same pushpoplock.

The VM runs as a service, so it shouldn't try to do AWT stuff, but it's done by a library I'm using.

Any ideas? Can I prevent this from happening?

Thanks!

Upvotes: 15

Views: 1111

Answers (2)

Vaspar
Vaspar

Reputation: 576

Is there any possibility that push(EventQueue newEventQueue) is called by your application program and that pushes the same eventQueue? If yes, then, this and its nextQueue will be same objects and they will be running in an endless loop consuming CPU to 100%.

From the stack trace its evident that at least one thread is running. So its not a question of DEADLOCK.

From the 100% CPU consumption hint and its state as RUNNABLE, its evident its doing an infinite loop.

The code can go into infinite loop if and only if the nextQueue has a value that's already in the chain (or this). It could very well be an application problem. Thanks.

Upvotes: 1

Coder55
Coder55

Reputation: 559

Your problem is a deadlock problem(see wikipeida).

Deadlock situation here As you can see above, your two EventQueues (here R1 and R2) are in a deadlock situation - each of them has claimed one resource and cannot run further while the other one did claim the other resource. Both are waiting for each other endless.

You can solve this problem with different approaches:

Try to change the library, you're right that a library shouldn't try to do awt stuff if it is not a library closely related to awt. Thereare many libraries on platforms like github, I'm quite sure you find another library to replace your one which causes the error.

If you are able to edit your code to add monitoring you are preventing any deadlocks, too.

synchronized(lock){
EventQueue next = eq.nextQueue;
while (next != null) {
    eq = next;
    next = eq.nextQueue;
}
lock.notifyAll();
}

Upvotes: 0

Related Questions