Background thread, daemon thread (Daemon thread)
The so-called background thread means that this thread is not part of the program, so when all non-background threads end, the program terminates and kills all the background threads in the process. The thread is set to a background thread by Setdaemon (true).
Packagecom.csdhsm.concurrent;ImportJava.util.concurrent.TimeUnit;/*** @Title: Simpledaemonthreaddemo.java * @Package: Com.csdhsm.concurrent * @Description to test the Daemon Thread * /c5>@authorHan * @date 2016-4-19 pm 3:56:51 *@versionV1.0*/ Public classSimpledaemonthreaddemoImplementsrunnable{@Override Public voidrun () { while(true) {System.out.println (Thread.CurrentThread ()); Try{TimeUnit.MILLISECONDS.sleep (50); } Catch(interruptedexception e) {System.out.println ("Sleep is intrrupted!"); } } } Public Static voidMain (string[] args) { for(inti = 0; I < 10; i++) {Thread T=NewThread (NewSimpledaemonthreaddemo ()); //set the thread to daemonT.setdaemon (true); T.start (); } Try { //wait other thread to runTimeUnit.MILLISECONDS.sleep (30); } Catch(Interruptedexception e) {} System.out.println ("Main thread is Over"); }}Results
It is clear to see that when the main thread ends, the other threads are no longer running.
Java concurrent Programming (three) background thread (Daemon thread)