Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I create a new instance of Java SwingWorker each time I want to invoke doInBackground()?

My extended SwingWorker class performs a potentially reoccurring background task which requires GUI originating input variables.

I see 2 coding options:

  1. 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?

  2. 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?

like image 834
Rubber Duck Avatar asked Jan 23 '13 08:01

Rubber Duck


People also ask

What happens when SwingWorker task is not finished?

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.

What is SwingWorker in java?

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.

Is SwingWorker thread safe?

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.

What is the use of doinbackground in SwingWorker?

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.

What is the use of SwingWorker subclass in Java?

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.

What is SwingWorker thread in Java?

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.

What is the life cycle of a SwingWorker in Java?

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.


2 Answers

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.

like image 80
MadProgrammer Avatar answered Sep 28 '22 23:09

MadProgrammer


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.

like image 32
Philipp Wendler Avatar answered Sep 29 '22 01:09

Philipp Wendler