4

I am using swing and in my application i needed to run many threads in parallel like checking the internet connectivity after every 5 secs, monitoring the filesystem changes, sycing files from server.

All the time consuming tasks like above are running in SwingWorker so that my GUI should not freeze.

Same time i need to run some other time consuming tasks such as uploading file to server. for this purpose i also used swingWorker. and then i submit all these swingworker to executerService for thread pooling so that they should not effect each other.

My executer service is like this. i thought 30 threads will be enough for me.

static ExecutorService threadExecutor;
threadExecutor = Executors.newFixedThreadPool(30);

and then i submit threads in the same service.

threadExecutor.submit(monitorinternetconnectivity); //submitting swingworker obejct

Some of the threads i submit at the start and some i add runtime, when i add at runtime, it does not complete the job or stop running their job, like monitoring internet connectivity.

Is there any way to have the same functionality like swing worker, or some best way to use multiple swingworker. and we should be able to add new swingwokers at runtime to executer service

user1329572
  • 6,176
  • 5
  • 29
  • 39
Asghar
  • 2,336
  • 8
  • 46
  • 79
  • 1
    I don't understand why you'd delegate the execution of a `SwingWorker` to an `ExecutorService`. A `SwingWorker` will not block the current thread since it will do all of its work in a background thread until it is done. When it is done, the task will be executed in the EDT. – user1329572 Apr 23 '12 at 13:21
  • `ExecutorService` is the wrong tool if the job is an ongoing task. A long-running task needs its own thread and that's it, no benefit from thread pooling, except that you'll end up with tasks waiting in the queue because all threads in the pool are busy doing ongoing tasks. – Marko Topolnik Apr 23 '12 at 13:51
  • Last I checked from my own experimentation, SwingWorkers use a limited thread pool of 10 threads. I don't know if this is a general rule, but it would be easy for you to write a test program to see what you come up with in your environment. – Hovercraft Full Of Eels Apr 23 '12 at 15:00
  • 1
    @HovercraftFullOfEels Your memory is correct. Look at the source code and see the `javax.swing.SwingWorker#MAX_WORKER_THREADS` field with constant value of 10. This is used to construct the thread pool – Robin Nov 17 '12 at 11:20

1 Answers1

1
  1. SwingWorker uses it's own ThreadPool.
  2. SwingWorker should be used for a long running task after (i.e. anything that required more than a couple of milliseconds) after which a GUI update is required.
  3. No calls to update gui elements outside the EDT should be done (i.e. from the SwingWorker.done() method)

If you generally follow the rules of accessing Swing components form inside the EDT (look here) then you shouldn't have a problem with locking. I suspect that the problem rather lies in your code but to be sure of that we should see it.

Daniel Hiller
  • 3,415
  • 3
  • 23
  • 33