I've got an application that uses 2 long-running SwingWorker tasks and I've just en开发者_开发知识库countered 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...
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.
Maybe a bit late but, there's a bug in the newer Java versions: SwingWorker deadlocks due one thread in the swingworker-pool
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.
精彩评论