[concurrency-interest] ThreadPoolExecutor with corePoolSize = 0

David Holmes dcholmes at optusnet.com.au
Wed Dec 20 18:48:56 EST 2006


Correction: making the thread a daemon isn't necessarily the right thing if
you want it to clear the queue before the application terminates. In that
case you'd need explicit lifecycle management to shutdown the pool.

David

David Holmes wrote:
>
> Sam,
>
> I agree that there is no simple way to do this in the Java 5 TPE. Only
> threads above "coreSize" have the idle-timeout that you are
> looking for, and
> they are only created after the queue is full and that doesn't fit your
> needs.
>
> What is the problem with having the one thread live forever? If
> there is no
> work for it then it consumes no CPU. If you are worried about application
> termination and don't have a good hook at which to shutdown the pool then
> define a ThreadFactory that provides a daemon thread. One thread
> should not
> cause any resource issues.
>
> Otherwise, you can always take the public domain version of the Java 6 TPE
> and use that instead.
>
> Cheers,
> David Holmes
>
> > -----Original Message-----
> > From: concurrency-interest-bounces at cs.oswego.edu
> > [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Sam
> > Berlin
> > Sent: Thursday, 21 December 2006 8:17 AM
> > To: concurrency-interest at cs.oswego.edu
> > Subject: [concurrency-interest] ThreadPoolExecutor with corePoolSize = 0
> >
> >
> > 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.
> >
> > Thanks,
> >  Sam
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list