My extended SwingWorker class performs a potentially reoccurring background task which requires GUI originating input variables.
I see 2 coding options:
To start a new instance of the class each time I use it and pass the variables to the constructor. I presume I should make sure there are not to many instances. If so how? multiton or some other method?
Update the variables and call execute again? If so how do I make sure i'm not interrupting?
Is one of these options the way to go or is there a better way?
If the SwingWorker object has not finished executing the doInBackground() method, the call to this method blocks until the result is ready. It is not suggested to call this method on the event dispatch thread, as it will block all events until it returns. cancels the task if it is still running.
SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing. Subclasses of SwingWorker must implement the doInBackground() method to perform the background computation.
Since Swing is not thread-safe by design, it's designer did provide couple of utility methods in SwingUtilities class to update any Swing component from a thread other thread Event Dispatcher Thread.
SwingWorker is designed for such tricky situations which provides communication on event dispatch thread. doInBackground (): This function contains our logic of the background task i.e. what we want our thread to do. This function runs on worker thread and is necessary to implement.
The SwingWorker subclass can define a method, done, which is automatically invoked on the event dispatch thread when the background task is finished. SwingWorker implements java.util.concurrent.Future. This interface allows the background task to provide a return value to the other thread.
Worker Threads and SwingWorker When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads. Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker.
There are three threads involved in the life cycle of a SwingWorker: Current thread: The execute () method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately. One can wait for the SwingWorker to complete using the get methods.
SwingWorker is non-entrant, meaning you can not execute it again, much in the same way Threads are.
From the JavaDocs
SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice.
Plase use option 1.
Immutable objects are normally easier to work with. For example, you prevent the problem that the variables are updated while the worker is still working, and you have to think less about memory visibility.
Object instantiation is quite cheap in Java, so this won't be a performance problem and you can create a new instance every time to need one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With