Tuesday, February 17, 2009

SwingWorker

While researching my first algorithm I came across SwingWorker which is my first step I've taken into threaded applications.

A summary of SwingWorker:
SwingWorker is designed for situations where you need to have a long running task run in the background thread and provide updates to the User Interface when done, or still processing.

Any subclass of SwingWorker must implement the doInBackground() method to perform the background computation.

The workflow of SwingWorker:
1. Current thread
The Execute() method is called on this thread.
(schedules the SwingWorker on the worker thread)
2. Worker Thread
The doInBackground() method is called.
3. Event Dispatch Thread
Invokes the process() and done() methods here and notifies and PorpertyChangeListeners on this thread.

Use at work:
We are using the SwingWorker to make a calculation that deals with the rendering of the graphics which does take some time, so that is why SwingWorker was used in this case.

Just something I learned.
Here is the javadoc
https://swingworker.dev.java.net/nonav/javadoc/index.html

2 comments:

jalna said...

Soooo, can I use it to do a pot roast while I'm steaming sweet potatoes at the same time? aunty jalna

austen.ito said...

Only if the potatoes get to his plate after you call invokeLater().