[concurrency-interest] On-demand Construction of Threads in ThreadPoolExecutor

Vitaly Davidovich vitalyd at gmail.com
Tue Jan 17 10:11:58 EST 2012


Perhaps because you don't know upfront how many threads will actually be
needed to service the workload and thus don't want to spin them up eagerly
(for perf and efficiency reasons) but rather adjust as work comes in? That
is, what if just 1 thread is enough but pool is configured to allow for
more ...

Sent from my phone
On Jan 17, 2012 10:06 AM, "Dr Heinz M. Kabutz" <heinz at javaspecialists.eu>
wrote:

> **
> A quick historical question.  What was the thinking behind constructing
> the threads in the ThreadPoolExecutor lazily?
>
> In the JavaDocs of ThreadPoolExecutor:
>
> *On-demand construction*
>     By default, even core threads are initially created and started only
> when new tasks arrive, but this can be overridden dynamically using method
> {@link ThreadPoolExecutor#prestartCoreThread} or {@link
> ThreadPoolExecutor#prestartAllCoreThreads}. You probably want to prestart
> threads if you construct the pool with a non-empty queue.
>
> I can think of two possible answers:
>
> Correctness: The author of this class did not want the ThreadPoolExecutor
> instance to escape into the threads before it had completed being
> constructed.  We should avoid letting "this" escape during construction.
>
> Performance: It might be better to throttle the construction of new
> threads.  Say someone creates a new fixed thread pool with 2000 threads,
> instead of waiting for them all to be created, we let each task submission
> create a thread until we reach the core size.
>
> Or perhaps it was neither of these two answers.  Maybe it was just easier
> to write it this way, so that one class could be used for both the cached
> and the fixed thread pool.
>
> Would love to hear the reasons for this :-)
>
> Regards
>
> Heinz
> --
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java(tm) Specialists' Newsletter"
> Sun Java Champion
> IEEE Certified Software Development Professionalhttp://www.javaspecialists.eu
> Tel: +30 69 72 850 460
> Skype: kabutz
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120117/9213f1b2/attachment.html>


More information about the Concurrency-interest mailing list