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();
});
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)
SwingWorkeris created as bridge betweensJava Essentials Classesand Swing by implementsFuture, and quite guarantee that outoput from methodsprocess,publishanddonewill be onEventDispatchThread2) you can invoke
SwingWorkerfromExecutor, this is most safiest methods how to create multithreading in Swing by implementsSwingWorker3) notice carefully with number or thread, because
Executordoesn’t care somehow aboutSwingWorkerslife_cycle4) another important notice and here
5) for listening states from
SwingWorkeryou have to implementsPropertyChangeListener