Daemon Threads
/ * * Daemon threads, or daemon threads, are generally run in the background, serving other threads and cannot exist separately */ Public class Test08 { Public Static voidMain (string[] args) {MyThread8 T1 =NewMyThread8 ("Daemon Thread"); System.out.println ("Is it a daemon thread?" "+t1.isdaemon ()); T1.setdaemon (true); System.out.println ("Is it a daemon thread?" "+t1.isdaemon ()); T1.start ();NewMyThread8 ("Rubbish"); for(inti =1; I <= -; i++) {System.out.println (Thread.CurrentThread (). GetName () +"****"+ i); } }} class MyThread8 extends Thread { PublicMyThread8 (String name) {Super(name); Setdaemon (true); Start (); } @Override Public voidRun () { while(true) {System.out.println (Thread.CurrentThread (). GetName () +"garbage collection is in progress!" "); } }}
Break Thread in
/* * interrupt () Break thread */ Public class Test09 { Public Static voidMain (string[] args) {MYTHREAD9 MT =NewMYTHREAD9 (); Thread thread =NewThread (MT,"First"); Thread.Start (); for(intI=1; i<= -; i++) {System.out.println (Thread.CurrentThread (). GetName () +"***"); }Try{Thread.Sleep ( the);//Main thread sleeps 3 seconds}Catch(Interruptedexception e) {E.printstacktrace (); }//In the sleep of the Cheng breakThread.Interrupt (); }} class MyThread9 implements Runnable { intnum =1; @Override Public voidRun () { while(true) {if(num==Ten){Try{System.out.println (Thread.CurrentThread (). GetName () +"Thread is about to fall asleep for 10 seconds"); Thread.Sleep (10000); }Catch(Interruptedexception e) {System.out.println ("I will be pounding awake .... ");//e.printstacktrace ();}} System.out.println (Thread.CurrentThread (). GetName () +"***"+ num++); } }}
Java Learning Note (40)-Daemon thread vs. Interrupt threads