ThreadPoolExecutor and finalization

Peter Levart peter.levart at
Tue Oct 31 08:25:25 UTC 2017

Hi David,

On 10/31/17 08:45, David Holmes wrote:
>>> The docs for TPE cover this in detail: [1]
>>> Finalization
>>>     A pool that is no longer referenced in a program AND has no 
>>> remaining threads will be shutdown automatically. If you would like 
>>> to ensure that unreferenced pools are reclaimed even if users forget 
>>> to call shutdown(), then you must arrange that unused threads 
>>> eventually die, by setting appropriate keep-alive times, using a 
>>> lower bound of zero core threads and/or setting 
>>> allowCoreThreadTimeOut(boolean).
>> I'm trying to understand the purpose of finalize() in TPE, but can't. 
>> I'm surely missing something. If the pool is no longer referenced AND 
>> there are no active threads, what is there left to shutdown() 
>> actually? All that remains is garbage that will eventually be GCed.
> Ummmmm .... I'm going to have to do some archaeology here ...
> David

I can imagine a thread pool where worker threads, while idling, don't 
have a reference to the pool so the pool can shutdown() itself when:

- the pool is no longer referenced AND
- there is no worker thread executing a task (i.e. all worker threads 
are idle)

In such state, the pool is not reachable and may be shutdown.

But it seems that TPE is not such a pool.

Regards, Peter

>> Regards, Peter
>>> David
>>> -----
>>> [1] 

More information about the core-libs-dev mailing list