There are 2 threads in Java, 1 are daemon threads, 1 are non-daemon threads
A daemon thread is a special thread that, when a non-daemon thread is present in the thread, is automatically destroyed by the daemon thread, and the typical daemon thread is the garbage collection thread.
Test as follows
1 PackageCom.cky.daemon;2 3 /**4 * Created by Edison on 2017/12/3.5 */6 Public classMythreadsextendsthread{7 Private intI=0;8 9 @OverrideTen Public voidrun () { One Super. Run (); A Try { - while(true) { -i++; theSystem.out.println ("i=" +i); -Thread.Sleep (1000); - } -}Catch(interruptedexception e) { + e.printstacktrace (); - } + } A}
PackageCom.cky.daemon;/*** Created by Edison on 2017/12/3.*/ Public classTesst11 { Public Static voidMain (string[] args) {Try{mythreads mythreads=Newmythreads (); //set it as the daemon thread, default to False, non-daemon threadMythreads.setdaemon (true); Mythreads.start (); Thread.Sleep (5000); System.out.println ("I'm leaving the thread object and I'm not printing anymore."); } Catch(interruptedexception e) {e.printstacktrace (); } }}
I=1i=2i=3i=4i=5i=60
1.11 Daemon Threads