本文介绍了SwingWorker和Executor之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SwingWorker在正在制作的应用程序上执行一些繁重的任务.尽管今天我遇到了Executor类和这个示例:

I am using SwingWorker to execute some heavy load tasks on an application I am making . Although today I got across the Executor class and this example :

Executors.newCachedThreadPool().execute(new Runnable() {
                    public void run() {
                        someTask();     
  });

有人可以解释为什么使用SwingWorker而不是上面的示例的原因吗?这是我目前使用SwingWorker的方式:

Can someone explain the reasons why one would use SwingWorker instead of the above example ? This is the way I am currently using SwingWorker :

SwingWorker worker = new SwingWorker() {            
protected Object doInBackground() {
    someTask();
return null;
}
};
worker.execute();

推荐答案

1)SwingWorker由工具Future创建为Java Essentials Classes和Swing之间的桥梁,并且相当保证方法processpublishdone将位于EventDispatchThread

1) SwingWorker is created as bridge betweens Java Essentials Classes and Swing by implements Future, and quite guarantee that outoput from methods process, publish and done will be on EventDispatchThread

2)您可以从Executor调用SwingWorker,这是创建通过实现SwingWorker

2) you can invoke SwingWorker from Executor, this is most safiest methods how to create multithreading in Swing by implements SwingWorker

3)注意数字或线程,因为Executor不在乎SwingWorkers life_cycle

3) notice carefully with number or thread, because Executor doesn't care somehow about SwingWorkers life_cycle

4)另一个重要的通知此处

4) another important notice and here

5)要监听来自SwingWorker的状态,您必须实现PropertyChangeListener

5) for listening states from SwingWorker you have to implements PropertyChangeListener

这篇关于SwingWorker和Executor之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:55