简体   繁体   中英

Daemon thread in java

我对守护程序线程感到困惑。在大多数站点中,它被写入,它在应用程序停止时终止但是如果应用程序连续运行会怎样。

... 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.)


FOLLOWUP

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. 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:

  1. When you successfully stop the tomcat server (eg "catalina.sh stop") then the JVM exits and all threads (daemon or otherwise) die.

  2. 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.)

  3. 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, then thread leaks and memory leaks are moot. Indeed, this is the classic workaround for leaks.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM