Reputation: 8800
I am confused about daemon threads.In most of the sites it is written that it terminates when the application is stopped but what if application is running continuously.
Upvotes: 3
Views: 5483
Reputation: 719641
... but what if application is running continuously.
Then the daemon thread just keeps running.
(This assumes that the thread in question doesn't return from its run()
method, or die as a result of an uncaught exception.)
The point of marking a thread as "daemon" is to tell the JVM that it doesn't need to wait for the thread to finish before initiating a shutdown. (Another approach to shutting down is to have some thread call System.exit()
. This initiates the shutdown even if there are other non-daemon threads.)
If that thread is running and i'm using it in a web app,would stopping the server effect it?
Yes. Assuming that "stopping the server" means doing something to cause the webserver / webcontainer JVM to exit, then any threads will die.
I have an application that is running continuously under Tomcat server and i inferred from the comments that the daemon thread created will not stop as the application is not stopping,but what if we try to stop the tomcat server directly,will that create memory leaks?
If you stop the webapp (but not the tomcat server) then the daemon thread will keep running. Later versions of Tomcat will produce warning messages about this.
If you want to make threads created by your webapp go away, you have to program your webapp to listen for the relevant context events and interrupt (or whatever) the threads to make them shut down.
If you shut down Tomcat, then everything goes away, including daemon threads:
When you successfully stop the tomcat server (e.g. catalina.sh stop
) then the JVM exits and all threads (daemon or otherwise) die.
Running catalina.sh stop
can fail, but the mere existence of a daemon thread won't cause it to fail.
A failure to stop can be due to some webapp getting stuck in its shutdown event handling, or it can be due to the Tomcat server being in a hung or non-responsive state. In at least some versions of Tomcat, the existence of a non-daemon thread is sufficient to cause the shutdown to fail.)
If you run catalina.sh stop -force
then the Tomcat instance is killed if it fails to shutdown in 5 seconds; see catalina.sh help
or the script's source.
When you shutdown and restart Tomcat, any thread leaks and memory leaks are moot. Indeed, this is the classic workaround for leaks.
Upvotes: 9
Reputation: 4192
Correct. The application will only exit when all non-daemon threads have completed. This does not hold for daemon threads.
To clarify:
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
// do something...
}
}
}
thread.setDaemon(true);
thread.start();
}
The above will quit immediately. However, if the thread.setDaemon(true) is omitted, the program will NOT terminate.
Upvotes: 5
Reputation: 692231
A daemon thread is stopped when its run method returns, or when there is no non-daemon thread running anymore in the JVM. If there is a non-daemon thread running forever, and the daemon thread's run method never returns, then the daemon thread also runs forever.
Upvotes: 2