SwingWorker
is used for the following purposes:
done()
method.publish()
and process()
methods.SwingUtilities.invokeLater()
can perform the above tasks as follows:
SwingWorker.execute()
method from the EDT, we can execute ExecutorService.submit(new MyRunnable())
as it will also create another thread which can execute long-running task.done()
method of case1) SwingUtilites.invokeLater(new RunnableToExecuteDoneMethodCode())
at the end of the task.process()
method of case1) SwingUtilites.invokeLater(new RunnableToExecuteProcessMethodCode())
at the place where we called publish()
method in case1.I am asking this question because the problem specified in question Java - SwingWorker - Can we call one SwingWorker from other SwingWorker instead of EDT can be solved by SwingUtilities.invokeLater()
but can't be solved with SwingWorker
SwingWorker
is a helper class -- it is not that you need to use it, but using it is much simpler and clearer than doing the same work by hand. (It also makes checking progress easier.) Note that it was added version 6 -- before then some people used a simpler class defined in the Swing Tutorial or did step similar to the ones you noted.