[concurrency-interest] ThreadPoolExecutor question

Dawid Kurzyniec dawidk@mathcs.emory.edu
Wed, 29 Dec 2004 17:29:54 -0500


Hello,

I would like to use a thread pool that is 1) bounded, but 2) does NOT 
keep any threads (and JVM) alive if there are no requests for 
sufficiently long time (e.g. 30s). When the pool gets contended (i.e. 
maximum number of used worker threads is reached), I would like it to 
start enqueuing subsequent tasks rather than rejecting them. The most 
intuitive approach - set core size to 0, maximum pool size to the 
bounding value, and the queue to some kind of LinkedQueue or ArrayQueue 
- fails: no tasks get executed at all (consistently with Javadoc but - 
IMHO - a little bit counterintuitively).

Another way to look at this: I want something like fixed thread pool 
(Executors.newFixedThreadPool()) but with threads dying off when they 
are idle for too long, and re-created when they are needed again.

Is there a simple way to achieve this, one might think, quite reasonable 
behavior? Or is it not so reasonable and should I abandon it, relying 
instead on unbounded pools or giving up on queuing?

Regards,
Dawid Kurzyniec