SwingWorker挂在Unsafe.park() [英] SwingWorker hangs at Unsafe.park()

查看:221
本文介绍了SwingWorker挂在Unsafe.park()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SwingWorker ,它在后台与服务器通信,然后更新 JFrame 。我正在调试我的应用程序并注意到即使在 SwingWorker 完成其工作之后,其线程仍然存在。它挂在 Unsafe.park(java.lang.Object)这是一个本机方法。我进一步研究了这一点,发现我的应用程序中的所有其他 SwingWorker 在完成后都做了同样的事情。我可以提供源代码,如果有人想要它,但我不认为这是必要的,因为问题似乎很一般。

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 SwingWorkers 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.

我在没有调试器的情况下运行应用程序,问题仍在发生。这是 SwingWorker 线程的转储:

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)

我制作了一个使用 SwingWorker的示例程序通常在应用程序中使用的方式。这个程序有同样的问题。以下是代码:

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();
    }
}


推荐答案

这是SwingWorker的正常行为,如果空闲,则线程应在10分钟后终止。它解释了这里

This is a normal behavior of SwingWorker, the Thread should terminate after 10 minutes if it is idle. It's explained here

这篇关于SwingWorker挂在Unsafe.park()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆