Reputation: 148
Recently I fixed a bug in Tomcat Shutdown. It was due to some thread hanging there when Tomcat try to shutdown itself.
What I did was something similar to this:
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "RxComputationThreadPool-" + counter.incrementAndGet());
t.setDaemon(true);
return t;
}
I found similar issue in other context:
Could anyone help to explain the pros and cons of using setDaemon?
Upvotes: 0
Views: 138
Reputation: 6531
setDaemon(true)
marks particular thread as daemon. Daemon threads are a special kind of threads - JVM doesn't have to wait until they're finished to be able to shut down (via finishing main()
method).
You may want daemon threads to do non-critical cleanup work, for example gathering some statistics.
It is hard to say what's its pros and cons, but I did my best to explain its idea and possible usage.
Upvotes: 3