views:

271

answers:

3

I've got an application that uses 2 long-running SwingWorker tasks and I've just encountered a couple of Windows computers with updated JVMs that only start one of the them. There are no errors indicated so I have to assume that the default thread pool has only a single thread and therefore the second SwingWorker object is getting queued when I try to execute it.

So, (1) how do I check check how many threads are available in the default SwingWorker thread pool, and (2) how do I add threads if I'm going to need more?

Anything else that I should know? This apparent single-thread thread-pool situation goes against all of my expectations.

I'm setting up a ThreadPoolExecutor but this seems so wrong...

+1  A: 

The default SwingWorker thread pool actually has 10 threads (defined in the private variable SwingWorker.MAX_WORKER_THREADS). If you're only seeing one of your tasks start, I'd check the state of the other thread with JConsole or some other similar tool.

There's no easy way to mess with the default SwingWorker thread pool (it's private and not exposed by any of the public methods). Adding SwingWorkers to your own thread pool is the expected way to handle it if the default pool doesn't work.

Sbodd
A: 

Under JDK 6, the default number of worker threads is 10.

You don't normally need to create a ThreadPoolExecutor, the default thread pool used by swing worker is usually sufficient. Creating a ThreadPoolExecutor can be dangrous, as some of the static methods only create a pool with a single thread.

You may want to add logging to these threads, if not already present, and inspect the logs. It may be some other synchronization problem is stopping your second background task.

mdma
Regarding logging, I didn't diagnose a thread pool issue until I added a System.out.println() statement at the top of each SwingWorker's doInBackground() method. This statement fails to print for the second task on the 2 problem machines. BTW, I ended up using a pool created with Executors.newCachedThreadPool() and it works fine. But I agree that if there are supposed be 10 threads then there is a different problem so I'll try to return to it.
Guy Lancaster
A: 

Maybe a bit late but, there's a bug in the newer Java versions: SwingWorker deadlocks due one thread in the swingworker-pool

keuleJ