Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute SwingWorker multiple times

I want to be able to use SwingWorker subclass multiple times. Is this possible?

I have read in the java doc:

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 750
Feras Odeh Avatar asked Oct 04 '10 11:10

Feras Odeh


People also ask

What happens when SwingWorker task is not finished?

Important Methods of SwingWorker. Attempts to cancel the execution of this task. This attempt will fail if the task has already been completed, has already been canceled, or could not be canceled for some other reason. If successful, and this task has not started when cancel is called, this task should never run.

When to use SwingWorker?

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.


2 Answers

One instance of a class implementing SwingWorker can be indeed ran only once. There are no limitations on instantiating as many instances as you need and running them.

like image 123
Boris Pavlović Avatar answered Oct 08 '22 05:10

Boris Pavlović


You cannot instantiate as many instances you need and execute them. In SwingWorker class exists javax.swing.SwingWorker.MAX_WORKER_THREADS = 10. So you can execute a maximum 10 instances. An instance is freed only it spend 10 minutes in idle time. Do not use SwingWorker instance as a Thread instance;it is not a thread.

like image 32
user2772139 Avatar answered Oct 08 '22 05:10

user2772139