[concurrency-interest] Unexpected bevahiour in ThreadPoolExecutor
dcholmes at optusnet.com.au
Thu Jan 4 18:20:50 EST 2007
You are right. This is a known "quality-of-implementation" issue with TPE
and is being addressed for Java 7. Meanwhile if you wrap your tasks so that
no exceptions can be thrown, then the worker threads won't terminate.
> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Andrew
> Sent: Friday, 5 January 2007 8:14 AM
> To: concurrency-interest at cs.oswego.edu
> Subject: [concurrency-interest] Unexpected bevahiour in
> I've been using a ThreadPoolExecutor with corePoolSize == maxPoolSize,
> expecting that I'd always have a fixed number of threads to operate on
> tasks in the queue. However, if a thread throws an exception and
> terminates unexpectedly, no thread is spawned to replace it unless it's
> the last thread in the pool, even if the task queue is full. Looking at
> the code, it seems that new threads are only spawned when execute is
> called. However, this behavior is at best unexpected (and I would
> consider it a bug), especially in the following scenario:
> You create a ThreadPoolExecutor (TPE) with corePoolSize =
> maxPoolSize = n.
> You then submit N >> n long-running tasks to the TPE at the start and
> never submit another task. If one of those long-running tasks throws an
> exception and terminates, a new thread is not respawned, leaving you with
> n-1 threads to service the large number of tasks left in the queue. If
> this happens frequently enough, you're left with only one thread in the
> pool (since workerDone will spawn a new thread if there is something in
> the queue and there are no more threads).
> Am I doing something wrong with my usage of the TPE? Surely the behavior
> I'm expecting (submit a bunch of tasks to a thread pool with a fixed
> number of threads at startup time and wait until they terminate) isn't
> unique. Are there any good solutions? Why wouldn't workerDone spawn
> new threads if there are less than corePoolSize threads running and there
> is work in the queue?
> Please include this line when replying.
> Clunk enough people on the head and we'll have a nation of lunkheads.
> -- Foghorn Leghorn
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
More information about the Concurrency-interest