Java - SwingWorker - 我们可以从其他SwingWorker而不是EDT调用一个SwingWorker [英] Java - SwingWorker - Can we call one SwingWorker from other SwingWorker instead of EDT

查看:135
本文介绍了Java - SwingWorker - 我们可以从其他SwingWorker而不是EDT调用一个SwingWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SwingWorker ,如下所示:

public class MainWorker extends SwingWorker(Void, MyObject) {
    :
    :
}



<我从EDT调用了上面的 Swing Worker

MainWorker mainWorker = new MainWorker();
mainWorker.execute();

现在, mainWorker 创建了10个实例一个 MyTask 类,这样每个实例都可以在自己的线程上运行,以便更快地完成工作。

Now, the mainWorker creates 10 instances of a MyTask class so that each instance will run on its own thread so as to complete the work faster.

但是问题是我想在任务运行时不时更新gui。我知道如果任务是由 mainWorker 本身执行的,我可以使用 publish() process()更新gui的方法。

But the problem is I want to update the gui from time to time while the tasks are running. I know that if the task was executed by the mainWorker itself, I could have used publish() and process() methods to update the gui.

但由于任务由不同于 Swingworker 线程,如何从执行任务的线程生成的中间结果更新gui。

But as the tasks are executed by threads different from the Swingworker thread, how can I update the gui from intermediate results generated by threads executing tasks.

推荐答案

SwingWorker的API文档提供了以下提示:

The SwingWorker's API documentation offers this hint:


此线程上的doInBackground()方法称为
。这是所有
后台活动应该发生的地方。
通知PropertyChangeListeners
关于绑定属性更改使用
firePropertyChange和
getPropertyChangeSupport()方法。
默认有两个绑定属性
可用:状态和进度。

The doInBackground() method is called on this thread. This is where all background activities should happen. To notify PropertyChangeListeners about bound properties changes use the firePropertyChange and getPropertyChangeSupport() methods. By default there are two bound properties available: state and progress.

MainWorker 可以实现 PropertyChangeListener 。然后它可以使用 PropertyChangeSupport 注册自己:

MainWorker can implement PropertyChangeListener. It can then register itself with its PropertyChangeSupport:

getPropertyChangeSupport().addPropertyChangeListener( this );

MainWorker 可以提供 PropertyChangeSupport 对象创建的每个 MyTask 对象。

MainWorker can supply its PropertyChangeSupport object to every MyTask object it creates.

new MyTask( ..., this.getPropertyChangeSupport() );

A MyTask 对象可以通知其<使用 PropertyChangeSupport.firePropertyChange 方法进行code> MainWorker 进度或属性更新。

A MyTask object can then notify its MainWorker of progress or property updates by using PropertyChangeSupport.firePropertyChange methods.

MainWorker ,如此通知,然后可以使用 SwingUtilities.invokeLater SwingUtilities.invokeAndWait 通过EDT更新Swing组件。

MainWorker, so notified, can then use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait to update the Swing components via the EDT.

protected Void doInBackground() {
    final int TASK_COUNT = 10;
    getPropertyChangeSupport().addPropertyChangeListener(this);
    CountDownLatch latch = new CountDownLatch( TASK_COUNT ); // java.util.concurrent
    Collection<Thread> threads = new HashSet<Thread>();
    for (int i = 0; i < TASK_COUNT; i++) {
        MyTask task = new MyTask( ..., latch, this.getPropertyChangeSupport() ) );
        threads.add( new Thread( task ) );
    }
    for (Thread thread: threads) {
        thread.start();
    }
    latch.await();
    return null;
}

这篇关于Java - SwingWorker - 我们可以从其他SwingWorker而不是EDT调用一个SwingWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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