SwingWorker 为什么会意外停止? [英] Why does SwingWorker stop unexpectedly?

查看:23
本文介绍了SwingWorker 为什么会意外停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用 SwingWorker 的一些想法,因为我还没有使用它太多.相反,我遇到了一个问题,我无法弄清楚出了什么问题.

I wanted to try out some ideas using SwingWorker since I haven't used it too much. Instead, I ran into an issue and I can't figure out what's wrong.

这里有一个简短的 SSCCE 演示了这个问题(我知道这里的人喜欢 SSCCE):

Here's a short SSCCE that demonstrates this problem (I know people here like SSCCEs):

import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class SwingWorkerTest
{
    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override
            public void run ()
            {
                new MySwingWorker (500).execute ();
                new MySwingWorker (900).execute ();
                new MySwingWorker (1200).execute ();
            }
        });
    }
}

class MySwingWorker extends SwingWorker<Void, Void>
{
    private int ms;

    public MySwingWorker (int ms)
    {
        this.ms = ms;
    }

    @Override
    protected Void doInBackground()
    {
        Thread t = Thread.currentThread ();

        for (int i = 0; i < 50; i++)
        {
            try
            {
                Thread.sleep (ms);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace ();
            }

            System.out.println ("I am thread with " + ms + " sleep in iteration " + i + ": " + t.getName () + " (" + t.getId () + ")");
        }

        return null;
    }
}

所以我的程序应该创建 3 个 SwingWorkers,在屏幕上打印一行,然后休眠指定的毫秒数,然后再次打印该行并再次休眠等.我从内部创建了 SwingWorkersSwing 的线程,否则他们甚至不会启动.

So my program should create 3 SwingWorkers that print a line on the screen then sleep for the specified number of milliseconds and then print the line again and sleep again etc. I create the SwingWorkers from within Swing's thread because otherwise they wouldn't even start.

所以预期的输出是:

I am thread with 500 sleep in iteration 0: SwingWorker-pool-1-thread-1 (15)
I am thread with 900 sleep in iteration 0: SwingWorker-pool-1-thread-2 (16)
I am thread with 500 sleep in iteration 1: SwingWorker-pool-1-thread-1 (15)
I am thread with 1200 sleep in iteration 0: SwingWorker-pool-1-thread-3 (17)
I am thread with 500 sleep in iteration 2: SwingWorker-pool-1-thread-1 (15)
I am thread with 900 sleep in iteration 1: SwingWorker-pool-1-thread-2 (16)
I am thread with 500 sleep in iteration 3: SwingWorker-pool-1-thread-1 (15)
I am thread with 1200 sleep in iteration 1: SwingWorker-pool-1-thread-3 (17)
I am thread with 500 sleep in iteration 4: SwingWorker-pool-1-thread-1 (15)
I am thread with 900 sleep in iteration 2: SwingWorker-pool-1-thread-2 (16)
I am thread with 500 sleep in iteration 5: SwingWorker-pool-1-thread-1 (15)
I am thread with 500 sleep in iteration 6: SwingWorker-pool-1-thread-1 (15)
I am thread with 900 sleep in iteration 3: SwingWorker-pool-1-thread-2 (16)
I am thread with 1200 sleep in iteration 2: SwingWorker-pool-1-thread-3 (17)
I am thread with 500 sleep in iteration 7: SwingWorker-pool-1-thread-1 (15)
.............

以此类推,总共 150 行(3 个工人 x 每个迭代 50)

and so on for a total of 150 lines (3 workers x 50 iterations for each)

相反,我得到的输出是:

Instead, the output I get is:

I am thread with 500 sleep in iteration 0: SwingWorker-pool-1-thread-1 (15)
I am thread with 900 sleep in iteration 0: SwingWorker-pool-1-thread-2 (16)
I am thread with 500 sleep in iteration 1: SwingWorker-pool-1-thread-1 (15)

就是这样.只需3行.在此之后,程序退出.try catch 块的任何地方都没有堆栈跟踪.

And that's it. Just 3 lines. After this the program exits. There's no stacktrace anywhere from that try catch block.

1). 1200 毫秒睡眠的工人在哪里?实际上,如果我用 1000ms 替换 1200ms,那么这个工人也会打印 1 行……是的,只有 1 行.奇怪...

1). Where is the worker with the 1200ms sleep ? Actually, if I replace 1200ms with 1000ms, then this worker also prints 1 line... yes, only one. Weird...

2). 工人为什么停下来?(我没有得到 150 行的东西)

2). Why do the workers stop ? (and I don't get 150 lines of stuff)

我在 64 位 Windows 7 上使用 JRE 7 Update 11 运行了这个程序.

I ran this program using JRE 7 Update 11 on Windows 7 64-bit.

PS:我很确定此处提到的错误已在此版本的 JRE 中修复,因为我确实这样做了获得 2 个不同的值(15 和 16)作为打印在控制台上的线程 ID.这是我怀疑的第一件事.

PS: I'm pretty sure that the bug mentioned here was fixed in this version of JRE, since I do get 2 distinct values (15 and 16) as thread ID's printed on the console. This was the first thing I suspected.

推荐答案

我相信您需要显示一个可视化的 Swing 顶级窗口,以保持 Swing 事件线程处于活动状态.否则程序会因为缺少非守护线程而关闭.

I believe that you need to show a visualized Swing top level Window in order to keep the Swing event thread alive. Otherwise the program will shut down for lack of non-daemon threads.


证明 SwingWorker 线程是 Daemon 线程,只需添加一行代码进行测试即可:


To prove that the SwingWorker thread is a Daemon thread, just add a line of code to test it:

System.out.println("I am thread with " + ms + " sleep in iteration "
   + i + ": " + t.getName() + " (" + t.getId() + ")");

// **** added
System.out.println("Daemon?: " + Thread.currentThread().isDaemon()); 

这篇关于SwingWorker 为什么会意外停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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