Multithreading with SwingWorkers in ExecutorService

Apotekarnes

Mitglied
Hello everybody,

the Java program I'm currently developing must allow parallelization in terms of multithreading.
My plan is to use ExecutorService thread pool in order to keep the number of concurrently running Threads at the level specified by the user.
Since the software will be featuring a complex GUI with Swing elements one day, which needs to stay responsive and receive updates from running background tasks, I decided to use SwingWorkers instead of standard Threads.
According to what I read, SwingWorkers spawn (up to 10) Threads without possibility to specify how many.
The big question is now, whether each SwingWorker submitted to the ExecutorService will potentially spawn ChildThreads or whether this is prevented by the ExecutorService?
In other words: do I lose control of how many threads are really used simultaneously?
When I set the number of Threads to 1 in initialization of the ExecutorService and submitted a SwingWorker, I observed already full consumption of all CPUs in my i5 laptop.

I hope somebody can help me here.

Thank you, and best regards,
Apotekarnes
 

Flown

Administrator
Mitarbeiter
If you really want to have control over every spawned Thread, then you should avoid SwingWorker. Because the first paragraph in the JavaDoc states:
An abstract class to perform lengthy GUI-interaction tasks in a background thread. Several background threads can be used to execute such tasks. However, the exact strategy of choosing a thread for any particular SwingWorker is unspecified and should not be relied on.
Therefore use your own implementation like the SwingWorker. I also recommend to read about concurrency in Swing applications.
 

Ähnliche Java Themen


Oben