[concurrency-interest] The default thread factory

Brian Goetz brian@quiotix.com
Thu, 8 Jan 2004 03:55:44 -0800


> The default thread factory returned by Executors creates user (not deamon)
> threads. As such, an ExecutorService which uses this default factory must
> have its shutdown method called so that the program may terminate.
> 
> I am wondering about the rationale for this decision. Why not use deamon
> threads by default? (I know that I can provide my own thread factory)

This was an issue of some debate in the Expert Group.  Here is one
of the arguments against making the default daemon=true.  Consider
this case: 

main() { 
  Executor e = Executors.newFixedThreadPool(N_THREADS);
  e.execute(aTask);
  e.execute(anotherTask);
  ...
  e.execute(oneMoreTask);
  e.shutdown();
}

With a thread factory which creates threads as daemon threads, if
there were no other threads besides the main thread, the program might
exit before all tasks had executed, in a confusing and race-prone
manner.