There are many sayings about Java Daemon threads on the Internet, and the readers are dizzy.
So I will summarize it myself:
Java can create two types of processes: "User thread" and "daemon thread"
User thread: the common thread we create at ordinary times.
Daemon thread: Mainly used to serve user threads.
How can we differentiate these two threads?
In fact, the JDK documentation clearly states that:
* The Java Virtual Machine exits when the only threads running are all * daemon threads.
That is:
When only the daemon thread is left in the thread, the JVM will exit. However, if any other user thread is still present, the JVM will not exit.
The following experiment shows that the JVM will exit when only the daemon thread is left in the thread.
The Code is as follows:
Public class DaemonRunner implements Runnable {public void run () {while (true) {for (int I = 1; I <= 100; I ++) {System. out. println (I); try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace () ;}}} public static void main (String [] args) {Thread daemonThread = new Thread (new DaemonRunner (); // set it to daemonThread. setDaemon (true); daemonThread. start (); System. out. pri Ntln ("isDaemon =" + daemonThread. isDaemon (); wait = new wait (System. in); // accept the input so that the program stops here. Once the user input is received, the main thread ends and the JVM exits! Vertex. next ();
// The AddShutdownHook method adds an event to be processed when JVM is stopped:
// Print the JVM Exit statement when the JVM exits. runtime. getRuntime (). addShutdownHook (new Thread () {@ Override public void run () {// TODO Auto-generated method stub System. out. println ("JVM Exit! ");}});}}
When the program runs, the Daemon thread constantly prints numbers on the console, while the main thread is a user thread because of "outputs. next ()"
Waiting for user input is blocked. At this time, the JVM will not exit.
Verification steps:
At this time, as long as we confirm that there is only one user thread, that is, as long as the main thread exits, and then check whether the JVM actually exits.
So how can we determine the number of current user threads and daemon in the current JVM Virtual Machine?
We can use the jstack stack tool that comes with JDK.
Jstack 3308 (that is, the ID of the JVM process to be tested)
The result is as follows:
We can see that the current JVM process runs a lot of multithreading, but only the main Thread is the user Thread, other threads are not the daemon Thread (such as Low Memory Detector or Thread-0 ), or Virtual Machine Internal Threads (such as VM Thread and VM Periodic Task Thread, we don't need to consider these threads ).
Therefore, when the main thread of the user thread exits (In this program, enter a character in the console and press ENTER). If the JVM does Exit, ShutDownHook will be called to print "JVM Exit!." on the console !."
If you do not exit, of course it will not be printed.
Verification Result:
The console successfully prints "JVM Exit !", Exit JVM!