public class Daemontest {public static void Main (string[] args) { new Workerthread (). Start (); try { thread.sleep (7500); } catch (Interruptedexception e) {} System.out.println ("Main Thread Ending"); }}class Workerthread extends Thread {public workerthread () { Setdaemon (true); When false, (i.e. when it's a user thread), //The Worker thread continues to run. When True, (i.e. when it's a daemon thread), //The Worker thread terminates when the main //thread Terminat Es. } public void Run () { int count=0; while (true) { System.out.println ("Hello from Worker" +count++); try { sleep () } catch (Interruptedexception e) {}} }}
Simply understood: The daemon does not prevent the JVM from shutting down. The JVM cannot shut down when a user thread is running. When no user thread is running, there is no daemon thread, and the JVM shuts down.
Daemon Threading Application Example: Java garbage collection. When no thread is running, garbage is not generated and garbage collection does not work, and the JVM can shut down.
Daemon threads Apply background: Background threads (such as threads that can collect certain system state, send email threads, etc. that do not want to affect the JVM)
The difference between a Java daemon thread and a user thread