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 aSwingWorker
more than once will not result in invoking thedoInBackground
method twice.
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.
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.
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.
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.
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