There are two types of threads in Java: The user thread and the daemon thread .
Set to User thread by Thread.setdaemon (false);
Set to the daemon thread by Thread.setdaemon (true).
If you do not set the secondary property, the user thread is the default.
Difference: The user thread will continue to run after the main thread ends, the JVM survives, and if there is no user thread at the end of the main thread, the JVM ends.
Public classMytestextendsThread { Public voidrun () { for(inti=0;; i++){ Try{Thread.Sleep (1000); } Catch(Exection ex) {} System.out.println (i); } } Public Static voidmain (String [] args) {Mytest test=NewMytest (); Test.setdaemon (true); Test.start (); System.out.println ("Isdaemon:" +Test.isdaemon ()); Try { //Test.setdaemon (TRUE); accepts input, once the user input is received, the main thread ends, the daemon thread ends automatically, and the JVM ends. //Test.setdaemon (FALSE); accepts input, once the user input is received, the main thread ends, the user thread continues, that is, the JVM does not end. System.in.read (); } Catch(IOException ex) {} }}
Java threads are divided into two types, the daemon thread and the user thread.