< what is a daemon thread, what is a non-daemon thread >
Java has two threads: "Daemon thread daemon" (daemon thread) and "User thread username" (Non-daemon thread).
User threads: non-daemon threads include regular user threads or event dispatch threads such as those used to handle GUI events, and the Java virtual machine automatically leaves after all its non-daemon threads have left.
Daemon Thread: The daemon thread is used to serve the user thread, such as a GC thread. If no other user thread is running, there is no service object, and there is no reason to go on. (Inside the operating system there is no concept of the so-called daemon, only the daemon, but the Java language mechanism is built on the basis of the JVM, meaning that the Java platform to the bottom of the operating system is shielded, so it can be in its own virtual platform to construct a self-beneficial mechanism, and language or platform designers are more or less affected by the Unix idea, and the daemon thread mechanism is the JVM such a platform, so the Guardian thread came into being;
Daemon threads use less, but are not useless, for example, the JVM's garbage collection, memory management, and other threads are daemon threads. There is in the database application, the use of the database connection pool, the connection pool itself contains a lot of background threads, monitoring the number of connections, time-out, status, and so on.
the only difference between a daemon thread and a user thread is that, in fact, the UI thread and the daemon thread daemon are fundamentally different, and the only difference is that the virtual machine leaves, and when all the threads in the JVM are daemons, The JVM can exit (if the user thread is fully evacuated, then the daemon thread does not have a thread to serve, so the virtual machine exits); if there is one or more non-daemons, it will not exit. (The above is for normal exit, call System.exit will definitely exit).
For example: Just like the security of Heaven and Earth (Guardian thread), there is a memorial girl (non-guardian thread), they can do their own work at the same time, but if the girls are all taken by JC, then the door of the security there is no sense of existence.
< How to create a daemon thread >
The daemon thread is basically no different than the normal thread notation, and the method that invokes the thread object is Setdaemon (true), which can be set as the daemon thread.
1, Thread.setdaemon (true) must be set before Thread.Start (), you cannot set the running regular thread as the daemon thread, otherwise you will run out a illegalthreadstateexception exception, If the thread is a daemon thread, the Isdaemon method returns True. (Note: This is a significant difference from the daemon, the daemon is created, let the process out of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; So the language mechanism pinned to the virtual machine is fundamentally different from the system-level language).
2. The new thread generated in the daemon thread is also daemon. (This is the essential difference: the Daemon fork () is no longer the daemon, although it copies the process-related information of the parent process, but the process of the child process is not the INIT process, the so-called Daemon is essentially "the parent process hangs, init adoption, Then file 0,1,2 are/dev/null, current directory to/")
3. Not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not come and is operating.
Detailed description of the Setdaemon method:
< daemon thread scheduling example >
//Example 1: The daemon thread task to complete the file outputImportJava.io.*; Public classTestdemo { Public Static voidMain (string[] args)throwsinterruptedexception {Runnable tr=Newtestrunnable (); Thread Thread=NewThread (TR); Thread.setdaemon (true);//Setting the daemon threadThread.Start ();//start of sub-process execution } classTestrunnableImplementsrunnable{ Public voidrun () {Try{Thread.Sleep (1000);//Daemon thread blocks after 1 seconds of runningFile f=NewFile ("Daemon.txt"); FileOutputStream OS=NewFileOutputStream (F,true); Os.write ("Daemon". GetBytes ()); } Catch(IOException E1) {e1.printstacktrace (); } Catch(interruptedexception E2) {e2.printstacktrace (); } } } }
Run Result: There is no "daemon" string in file Daemon.txt.
But if you put Thread.setdaemon (true); Set the daemon thread to comment out that the file Daemon.txt can be written to the daemon string
The standard for the JRE to determine whether the program is executed is that all the foreground thread octave is complete, regardless of the status of the background thread, so be sure to pay attention to this issue when using background threads.
Example 2: Scheduling of threads-Daemon threads
Public classTest { Public Static voidMain (string[] args) {Thread T1=NewMycommon (); Thread T2=NewThread (NewMydaemon ()); T2.setdaemon (true);//Set as daemon threadT2.start (); T1.start (); } } classMycommonextendsThread { Public voidrun () { for(inti = 0; I < 5; i++) {System.out.println ("Thread 1" + i + "times executed! "); Try{Thread.Sleep (7); } Catch(interruptedexception e) {e.printstacktrace (); } } } } classMydaemonImplementsRunnable { Public voidrun () { for(Longi = 0; i < 9999999L; i++) {System.out.println ("Background line Cheng Di" + i + "time to execute!" "); Try{Thread.Sleep (7); } Catch(interruptedexception e) {e.printstacktrace (); } } } }Run Result: The/*********************************** background thread executes for the NO. 0 time!
Thread 1 executes for the NO. 0 time!
Thread 1 executes for the 1th time!
The background thread executes for the 1th time!
The background thread executes for the 2nd time!
Thread 1 executes for the 2nd time!
Thread 1 executes for the 3rd time!
The background thread executes for the 3rd time!
Thread 1 executes for the 4th time!
The background thread executes for the 4th time!
The background thread executes for the 5th time!
The background thread executes for the 6th time!
The background thread executes for the 7th time!
Process finished with exit code 0**********************************/can be seen from the above results: the foreground thread is guaranteed to execute, The background thread has not finished executing before exiting (the JVM exits when the current thread is all daemon threads). ---------------------------------------------------------------------------------------Note: This article is reproduced in:/HTTP blog.csdn.net/wjh5240313226/article/details/51501941, thanks to the original author.
Daemon and non-daemon Threads in Java (reprinted)