Multithreading - daemon thread

       Daemon thread is a special kind of thread, generally used to deal with some background operations, such as garbage collection thread JDK, then let's talk about this issue?

To understand daemon thread before, we have to figure out a particularly important issue: JVM quits under what circumstances?

The Java Virtual Machine exits when the only threads running are all daemon threads .

This sentence from the JDK's official documentation, of course, this phrase refers to the normal exit, instead of calling the System, .exit () method, described by these words, we find that, under normal circumstances If the JVM is not a non-daemon thread, the JVM will exit the process.

 

To understand the daemon thread through a simple procedure

public class DeamonThread {


    public static void main(String[] args) throws InterruptedException {
        //1 main 线程开始
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //② 将thread 设置为守护线程
        //thread.setDaemon(true);

        //3 启动线程
        thread.start();

        Thread.sleep(2_000L);
        System.out.println("Main thread finished lifecycle");
        //4 结束线程
    }

}

 

       The presence of two threads above code, a JVM is started by the main thread, the other is our own to create your own threads Thread, run the main method, you will find that the JVM process will never quit, even if the main thread is normally ended their life cycle (main thread of the life cycle is part of the code between 1 and 4), the reason is because there are still in the JVM process a non-daemon thread running.

       If you turn ② comments by setDaemon (true) method to set the thread daemon thread, so the main end of the process life cycle, JVM will follow out of operation, of course thread thread will end.

 

note:

  1. Daemon thread method is very simple, you can call the method setDaemon, true representatives daemon thread, false representative of normal thread
  2. Whether the thread is a daemon thread threads have a great relationship with the parent, if the parent thread is a normal thread, the thread is normal subclasses, and vice versa, if you want to modify its properties can help setDaemon method, isDaemon method to determine the thread It is not a daemon thread
  3. setDaemon method is only set to take effect before the thread is started, if a thread after a death, then another set setDaemon will throw an exception IllegalThreadStateException

 

The role of guardian of the thread

       If the JVM process is not a non-daemon thread, the JVM exits, that daemon threads have characteristics automatically ends the life cycle, rather than a daemon thread not have this feature, imagine if the thread garbage collection process is a non-JVM guardian thread, if the main method to complete the work, the JVM can not quit, because garbage recovery is still work properly.

      Daemon thread is often used as the background and perform some tasks, therefore sometimes it is also called a background thread, when you want to turn off some threads, or when to exit the JVM process, some threads can automatically shut down, then you can consider use daemon thread to complete your work.

 

<< java from high concurrent programming Detailed >>

 

Guess you like

Origin blog.csdn.net/qq_40646143/article/details/93739053