Mingtao Zhang
Mingtao Zhang

Reputation: 148

Thread setDameon(true) hanging Tomcat Shutdown

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:

https://github.com/craigmingtaozhang/RxJava/blob/master/rxjava-core/src/main/java/rx/schedulers/EventLoopsScheduler.java

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:

https://issues.apache.org/jira/browse/KAFKA-1305?jql=project%20%3D%20KAFKA%20AND%20resolution%20%3D%20Unresolved%20AND%20priority%20%3D%20Blocker%20ORDER%20BY%20key%20DESC

Could anyone help to explain the pros and cons of using setDaemon?

Upvotes: 0

Views: 138

Answers (1)

Alexey Malev
Alexey Malev

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

Related Questions