The so-called background (daemon) thread, also known as the daemon thread, refers to a thread that provides a generic service in the background (for example, a daemon thread GC) while the program is running, and this thread is not part of the program, so when all non-background threads end, The program also terminates, killing all background threads at the same time. Conversely, the program does not terminate as long as there are any non-background threads (for example: the non-daemon thread, main ()). Background threads have the lowest priority.
The Setdaemon (Boolean on) method makes it easy to set the thread's daemon mode, true to daemon mode, and default (FALSE) to user mode. The Setdaemon (Boolean on) method must be called before the thread is started (Start ()), and an exception is generated when threads are running.
The Isdaemon method tests whether the thread is a daemon thread. Note: When you create other threads in a daemon thread, these newly generated threads do not have to set the Daemon property, they will be the daemon thread, and the user thread is the same.
The only difference between a daemon thread and a normal thread is that the daemon is the main die, and when main exits, it terminates , and the normal thread stops at the end of the task execution .
Normal Thread Code:
1 PackageCom.mianshi.easy;2 Public classStartruntest {3 /**4 * Background (daemon) threading Experiment5 */6 Public Static voidMain (string[] args) {7Thread thread=NewThreaddemo ();8 9 //1, why did not print out 100 sentences? Because we set the thread thread to Daemon (daemon) thread, it is possible to exit the program only when the daemon thread exists, so only seven sentences are printed out.Ten //2. When the Java virtual machine has a daemon thread running, the Java virtual opportunity is turned off. When all regular threads have finished running, One //the virtual machine exits running regardless of where the daemon is running. So it's best that your daemon doesn't write business logic that will affect the program. Otherwise, you can't predict what's going to happen to the program. A //Thread.setdaemon (true); - Thread.Start (); - } the - Public Static classThreaddemoextendsthread{ - @Override - Public voidrun () { + for(inti = 0; I < 100; i++) { -System.out.println ("This is a Thread test" +i); + } A } at } -}
View Code
Results:
This was a thread test0this is a thread test1this are a thread test2this is a thread test3this are a thread test4this is a Th Read Test5this is a thread test6this are a thread test7this is a thread test8this are a thread test9this is a thread test10t A thread test11this is a thread test12this are a thread test13this is a thread test14this are a thread test15this is A thread test16this is a thread test17this are a thread test18this is a thread test19this are a thread test20this is a Threa D test21this is a thread test22this are a thread test23this is a thread test24this are a thread test25this is a thread test2 6This is a thread test27this are a thread test28this is a thread test29this are a thread test30this is a thread test31this I s a thread test32this is a thread test33this are a thread test34this is a thread test35this are a thread test36this is a Thr EAD Test37this is a thread test38this are a thread test39this is a thread test40this are a thread test41this is a thread tes T42this is a Thread test43This is a thread test44this are a thread test45this is a thread test46this are a thread test47this is a thread test48this is a thread test49this is a thread test50this are a thread test51this is a thread test52this are a thread test53this is a Th Read Test54this is a thread test55this are a thread test56this is a thread test57this are a thread test58this is a thread te St59this is a thread test60this are a thread test61this is a thread test62this are a thread test63this is a thread test64thi S is a thread test65this is a thread test66this are a thread test67this is a thread test68this are a thread test69this is a Thread test70this is a thread test71this are a thread test72this is a thread test73this are a thread test74this is a thread Test75this is a thread test76this are a thread test77this is a thread test78this are a thread test79this is a thread test80t A thread test81this is a thread test82this are a thread test83this is a thread test84this are a thread test85this is A Thread test86this is a Thread test87this is a thread test88this are a thread test89this is a thread test90this are a thread test91this is a thread Test92this is a thread test93this are a thread test94this is a thread test95this are a thread test96this is a thread test97 This was a thread test98this is a thread test99
View Code
It can be seen that the default normal thread is started in the main thread, then the main thread does not have any execution statements, start waiting for the child threads to execute, after the child threads get CPU execution, start executing the Run () method, after printing 100 sentences, the thread execution completes and terminates, at this time, the main thread will also end. All non-daemon threads in the program are completed and the program ends.
Daemon Thread Code:
1 PackageCom.mianshi.easy;2 Public classStartruntest {3 /**4 * Background (daemon) threading Experiment5 */6 Public Static voidMain (string[] args) {7Thread thread=NewThreaddemo ();8 9 //1, why did not print out 100 sentences? Because we set the thread thread to Daemon (daemon) thread, it is possible to exit the program only when the daemon thread exists, so only seven sentences are printed out.Ten //2. When the Java virtual machine has a daemon thread running, the Java virtual opportunity is turned off. When all regular threads have finished running, One //the virtual machine exits running regardless of where the daemon is running. So it's best that your daemon doesn't write business logic that will affect the program. Otherwise, you can't predict what's going to happen to the program. AThread.setdaemon (true); - Thread.Start (); - } the - Public Static classThreaddemoextendsthread{ - @Override - Public voidrun () { + for(inti = 0; I < 100; i++) { -System.out.println ("This is a Thread test" +i); + } A } at } -}
View Code
Results:
1
View Code
At this point, go to the main thread, create a thread, then set it as a background thread, and call the start () method to make the child thread ready, but at this time, there is no non-background thread in the program, so the main thread can end and the program will stop.
What is a background (daemon) thread?