Difference between a daemon thread and a low priority thread

前端 未结 4 1111
忘了有多久
忘了有多久 2020-12-06 11:56

Recently I was asked a question:

We\'ve got the setPriority() method to set a thread for low priority. Then why do we need a daemon thr

相关标签:
4条回答
  • 2020-12-06 12:34

    A running daemon thread will not prevent your program from ending/exiting. However, all user threads must end before your program can exit. Priority may apply to either daemon or user thread. You may understand priority the same way you understand it in everyday life.

    0 讨论(0)
  • 2020-12-06 12:43

    An example of

    1. JVM shutting down when low priority thread completes. Despite Daemon threads still running
    2. ALSO, shows that thread created by a daemon thread automatically becomes a daemon thread

      package junk.daemon_thread_example;
      
      class DeamonThreadPlus implements Runnable{
          String id;
          boolean createChild;
      
          public DeamonThreadPlus(String id, boolean createChild){
              this.id = id;
              this.createChild = createChild;
          }
      
          @Override
          public void run() {
              // Parent daemon thread creates child daemon thread by default
              if (createChild)
                  new Thread(new DeamonThreadPlus("DaemonChild", false)).start();
      
              // This thread never self-completes (only terminates when process dies)
              while (true){
                  try {
                      Thread.sleep(1);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("Daemon " 
                          + Thread.currentThread().isDaemon()
                          + " id = " + id);
                  }
          }
      }
      
      class UThread implements Runnable{
      
          @Override
          public void run() {
              System.out.println("User thread start");
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("User thread end (program exits)");
          }
      }
      
      public class Caller{
      
          public static void main(String[] args) {
              Thread dt = new Thread( new DeamonThreadPlus("Daemon", true));
              dt.setDaemon(true);
              dt.start();
      
              Thread ut = new Thread(new UThread());
              ut.setPriority(Thread.MIN_PRIORITY);
              ut.start(); 
          }
      
      }
      

      The output is: User thread start
      Daemon true id = Daemon
      Daemon true id = DaemonChild
      Daemon true id = Daemon
      Daemon true id = DaemonChild
      Daemon true id = Daemon
      Daemon true id = DaemonChild
      Daemon true id = Daemon
      Daemon true id = DaemonChild
      User thread end (program exits)
      Daemon true id = DaemonChild
      Daemon true id = Daemon

    0 讨论(0)
  • 2020-12-06 12:44

    If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.

    except that in daemon thread .. when JVM terminate abruptly then finally blocks are not executed, stacks are not unwound – JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.

    0 讨论(0)
  • 2020-12-06 12:46

    We've got the setPriority() method to set a thread for low priority. Then why do we need a daemon thread. What's the difference between them?

    Typically, daemon threads have nothing to do with priority. The JVM shuts down when all user non-daemon threads finish. Marking a thread as a daemon thread means that it can be safely killed when the JVM exits.

    Priority is about scheduling – about how often a thread gets a time slice in comparison to other threads that are ready to run. You can have low priority daemon threads or high priority daemon threads. You can have non-daemon threads that are also low and high priority. As an aside, thread priorities only apply in certain specific situations and on certainly architectures and as a Java thread expert, I never use them.

    The concepts are orthogonal (mutually independent) – at least in the Java thread model.

    In terms of when to make a thread daemon, I use daemon threads for any tasks that I don't care if they are interrupted when the JVM quits: keep-alive threads, statistics processors, log handling, etc.. Everything mission critical to the application is a non-daemon thread that has to be specifically interrupted or signaled to quit somehow.

    0 讨论(0)
提交回复
热议问题