I have a SwingWorker
that communicates with a server in the background and then updates a JFrame
. I was debugging my app and noticed that even after the SwingWorker
finished its work, its thread still remained. It is hanging at Unsafe.park(java.lang.Object)
which is a native method. I looked in to this further and found that all my other SwingWorker
s in my app do the same thing after they finish. I can provide source code if someone wants it but I don't think it is necessary because the problem seems to be very general.
Update
I ran the app without the debugger and the problem is still happening. This is the dump of the SwingWorker
thread:
"SwingWorker-pool-2-thread-1" daemon prio=6 tid=0x03219800 nid=0xd74 waiting on
condition [0x04b7f000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x22ec63d8> (a java.util.concurrent.locks.Abstra
ctQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject
.await(Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I made a sample program that uses SwingWorker
the way it is normally used in a application. This program has the same problem. Here is the code:
package swingworkerlocktest;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class SwingWorkerLockTest {
public static void main(String[] args) {
final JFrame frame = new JFrame("Frame");
final JTextArea outputArea = new JTextArea(4, 20);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(outputArea);
frame.pack();
frame.setVisible(true);
(new SwingWorker<Object, String>() {
@Override
protected Object doInBackground() throws Exception {
publish("Background task.");
return null;
}
@Override
protected void process(List<String> chunks) {
for (String str : chunks) {
outputArea.append(str + "\n");
}
}
@Override
protected void done() {
outputArea.append("Background task finished.");
}
}).execute();
}
}