[concurrency-interest] ThreadPoolExecutor with corePoolSize = 0

Sam Berlin sberlin at gmail.com
Wed Dec 20 17:17:04 EST 2006

Hi Folks,

We're interested in creating a ThreadPoolExecutor that lives with zero
live threads but will create a single thread and run any offered or
queued tasks when there are tasks to be run.  The rationale behind
this is to run occasional long-lived tasks that aren't time-critical
(and some requiring they be processed sequentially).  This is
different than the executor offered by Executors.singleThreadExecutor
because we don't want the thread it creates to live forever.

It appears that this is impossible, because using a core pool size of
0 causes no threads to ever be created.  (This is because of code in
execute(Runnable) that returns if poolSize < corePoolSize &&
addIfUnderCorePoolSize(runnable) both return true.
addIfUnderCorePoolSize doesn't add the thread because the core pool
size is 0, which the current size is not below.)

We unfortunately cannot use Java 1.6's corePoolSize timeout (which I
suspect would allow things to work by setting the core size to 1 and
adding a timeout).

Is there a way of doing this in Java 1.5?  Subclassing and adding
hooks will work fine, if it's possible to do it that way.


More information about the Concurrency-interest mailing list