[concurrency-interest] daemon or not

Brian Goetz brian@quiotix.com
Fri, 28 Nov 2003 08:24:22 -0800


> I see that the recently added defaultThreadFactory() creates Threads as 
> non-daemon threads. I wonder if creating them as daemon would be a 
> better default.

I wondered that too, at first, but I came to the conclusion that this is 
better.  

ThreadFactory is used by the various Executor classes.  Since
Executors are used to implement task queues, and task producers are
expected to outstrip task consumers at least some of the time, having
the executor threads go away when the non-daemon threads go away could
leave unprocessed, or partially processed, tasks.  This is a bug
waiting to bite you.

The ExecutorService interface includes methods for "shutdown now", and
"shutdown when currently queued tasks are finished."  This allows for
deterministic application control.  And you can always create threads
as daemon threads if you want.

When choosing between the safe thing and the convenient thing, in a
facility like j.u.c, the safe thing is almost always a better default.