[jmm-dev] finalization

Martin Buchholz martinrb at google.com
Sat Aug 16 21:54:15 UTC 2014

On Sat, Aug 16, 2014 at 7:16 AM, Doug Lea <dl at cs.oswego.edu> wrote:

> We've seen examples posted of the first three. The only
> usage of finalize() in java.util.concurrent, class
> ThreadPoolExecutor (TPE) is an example of the fourth.
> It invokes shutdown() just to be helpful about triggering early
> resource reclamation of possibly many Threads. But users cannot
> rely on TPEs being auto-shutdown in a way that overcomes the JVM
> rule that main programs cannot terminate until all other non-daemon
> threads terminate, so must in general call tpe.shutdown() before
> exit. Some not-very-relevant further notes for the curious:

I agree that daemon threads are a better default.

Users do have the choice of using a TPE with a daemon thread factory.

And when a java process's job includes all the tasks executing in a TPE,
the main thread should be doing something like tpe.awaitTermination anyways.

>  * The other j.u.c thread pool, ForkJoinPool uses daemon threads,
>    and not finalize(), but also kills off threads internally
>    after periods of non-use.  When available, this is a better
>    option for dealing with the JVM shutdown rule.

More information about the jmm-dev mailing list