Original address: http://www.cnblogs.com/ChrisWang/archive/2009/11/28/1612815.html
Understanding of Java Daemon Threading
There are many daemon threads on the web that look at people's dizziness.
So I'm going to summarize it myself:
The Java language can create two types of process "user thread" and "daemon thread" itself.
User thread: Is the normal thread that we normally create.
Daemon thread: Primarily used to serve user threads.
So how do you differentiate between these two threads?
In fact, it is clear from the documentation of the JDK:
The Java Virtual machine exits if the only threads running is all * Daemon threads.
That
The JVM exits when the thread is left with only the daemon. But if any other user thread is still there, the JVM will not exit.
Here we experiment to show that the JVM exits when the thread is left with only the daemon thread.
The code is as follows:
public class DaemonrunnerImplements Runnable {public void Run () {while (True) {for (int i = 1; i <=; 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 to Daemon Daemonthread.setdaemon (true); Daemonthread.start (); System. out.println (new Scanner (System. in); //accepts input, so the program pauses, once the user input is accepted, the main thread ends, and the JVM exits! Scanner.next ();
The Addshutdownhook method increases the processing event to be handled when the JVM stops:
when the JVM exits, the JVM Exit statement is printed. Runtime.getruntime (). Addshutdownhook (new Thread () { @Override public void Run () { auto-generated method Stub System. out.println ("JVM exit!");}); }}
When the program is running, the daemon thread will continuously print the number in the console, while the main thread is a user thread because of the "scanner.next ()"
Waiting for user input, is blocked. The JVM will of course not quit at this time.
Verification steps:
This time as long as we determine that the current user thread has only one, that is, as long as the main thread exits, and then check if the JVM really exits.
So how do we determine what the current user thread and daemon are in the current JVM virtual machine?
We can jstack with the view stack tool that comes with the JDK.
Jstack 3308 (that is, the process ID of the JVM process currently being probed)
The results were as follows:
We will see many threads running in the current JVM process, but only the main thread is the user thread, and the other threads are either daemon threads (such as low Memory detector or Thread-0) or internal threads of the virtual machine (such as VM thread and VM Periodic Task thread, these threads we don't have to consider).
So when the user thread exits the main thread (in this program, enters a character in the console and then returns ), if the JVM does exit, it calls Shutdownhook to print "JVM exit!." On the console.
If you don't quit, of course you won't print.
Validation results:
The console successfully prints "JVM exit!" and the JVM exits!
"Go" understanding of Java Daemon Threads