如何等待 SwingWorker 的 doInBackground() 方法? [英] How do I wait for a SwingWorker's doInBackground() method?

查看:22
本文介绍了如何等待 SwingWorker 的 doInBackground() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

import java.lang.InterruptedException;
import javax.swing.SwingWorker;

public class Test
{
    private JDialog window;

    public Test
    {
        // instantiate window
    }

    private class Task extends SwingWorker<Void, Void>
    {
        public Void doInBackground()
        {
            try { Thread.currentThread().sleep(5000); }
            catch(InterruptedException e) {}
            return null;
        }
    }

    public void doTask()
    {
        Task task = new Task();
        task.execute();
    }

    protected void process()
    {
        // update various GUI components here
    }

    public static void main(String args[])
    {
        Test t = new Test();
        t.doTask();
        System.out.println("done");
    }
}

我需要等到 t.doTask() 完成后才能打印出完成",但我不确定具体如何操作.我知道我应该在这里使用 join(),但是我需要一个线程来调用它,我不知道如何获取 doInBackground() 的线程我需要从那里调用 join().感谢您的帮助.

I need to wait until t.doTask() is done before printing out 'done', but I'm not sure exactly how. I know I should probably use join() here, but I need a thread to call it on, and I don't know how to get doInBackground()'s thread from where I need to call join(). Thanks for any help.

感谢您的回复.不幸的是,get() 等并不能完全解决问题.在我的实际代码中,SwingWorker 还有一个重写的 process() 函数,它在后台线程运行时更新 GUI 窗口.get() 确实会在 doInBackground 之后停止打印完成",但随后 GUI 不会更新.我更新了我的示例代码以反映这一点,尽管现在它当然不会编译.

Thanks for the responses. Unfortunately, get() and the like don't quite solve the problem. In my actual code, the SwingWorker also has an overridden process() function that updates a GUI window while the background thread is running. get() does stop 'done' from being printed till after doInBackground, but then the GUI doesn't update. I updated my sample code to reflect this, although now of course it won't compile.

有没有办法在 doInBackground 完成后才完成"打印?GUI 更新代码和完成"语句是否在同一个线程上?我需要创建一个新线程吗?

Is there a way to get 'done' to print only once doInBackground is finished? Are the GUI update code and the 'done' statement on the same thread? Do I need to make a new thread?

推荐答案

通常在 SwingWorker 完成其后台工作后需要做的任何事情都是通过重写 done() 方法.完成后在 Swing 事件线程上调用此方法,允许您更新 GUI 或打印一些内容或其他内容.如果你确实需要阻塞直到它完成,你可以调用 get().

Typically anything that needs to be done after a SwingWorker completes its background work is done by overriding the done() method in it. This method is called on the Swing event thread after completion, allowing you to update the GUI or print something out or whatever. If you really do need to block until it completes, you can call get().

注意.在 done() 方法中调用 get() 将立即返回您的结果,因此您不必担心会阻塞任何 UI 工作.

NB. Calling get() within the done() method will return with your result immediately, so you don't have to worry about that blocking any UI work.

这篇关于如何等待 SwingWorker 的 doInBackground() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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