Background thread (daemon thread) and background thread daemon
There is a Thread that runs in the background, and its tasks provide services for other threads. Such threads are called "Daemon Threads ), also known as "daemon thread ".
A typical background thread is a Timer "Timer" thread that sends a fixed interval to other threads.
Background threads are often used to handle the aftermath of a task. In addition, the background thread has a lower priority than other threads.
Compared with the background thread, a general thread is called a "User thread ". If only the background thread is running in an application, JVM will exit the application.
You can use setDaemon (boolean d) to set a common thread as a background thread. IsDaemon () can be used to test whether a specific thread is a background thread.
Package com. liujl. thread; public class DaemonThread extends Thread {@ Override public void run () {while (true) {System. out. println ("DaemonThread is running... ") ;}}; public static void main (String [] args) {DaemonThread dt = new DaemonThread (); // dt. setDaemon (true); dt. start ();}/*** dt. setDaemon (true): If the dt thread is set as the daemthread, start () is called. * after the dt thread is executed for a period of time, it will automatically exit, this is because when the thread scheduler detects that only * a background thread is running, it terminates the running of the referenced program. ** The reader can remove the 13th lines of code and run the reference again. At this time, the application will run continuously. */}