1. Set the thread is a background thread
note, main thread non-background threads, main thread is finished, the background thread no matter whether the end will kill non-background threads
1 /** 2 * 后台线程 3 */ 4 public class SimpleDaemons implements Runnable { 5 public void run() { 6 try { 7 while (true) { 8 TimeUnit.MILLISECONDS.sleep(100); 9 print(Thread.currentThread() + " " + this); 10 } 11 } catch (InterruptedException e) { 12 print("sleep() interrupted"); 13 } 14 } 15 public static void main(String[] args) throws Exception { 16 for (int i = 0; i < 10; i++) { 17 Thread daemon = new Thread(new SimpleDaemons()); 18 //设置为后台线程 19 daemon.setDaemon(true); // Must call before start() 20 daemon.start(); 21 } 22 print("All daemons started"); 23 TimeUnit.MILLISECONDS.sleep(1750); 24 } 25 }
2, the background thread properties
a. If a thread is a background thread, then any thread created by it will automatically be set to a background thread
1 / * 2 * background thread if one thread, then any thread created by it will automatically be set to a background thread . 3 * / . 4 class Daemon the implements the Runnable { . 5 Private the Thread [] T = new new the Thread [10 ]; . 6 public void RUN () { . 7 for ( int I = 0; I <t.length; I ++ ) { . 8 // derived child threads t [i] are default background thread . 9 t [i] = new new the thread ( new new DaemonSpawn ()); 10 T [I] .start (); . 11 printnb("DaemonSpawn " + i + " started, "); 12 } 13 for (int i = 0; i < t.length; i++) 14 printnb("t[" + i + "].isDaemon() = " + t[i].isDaemon() + ", "); 15 while (true) 16 Thread.yield(); 17 } 18 } 19 20 class DaemonSpawn implements Runnable { 21 public void run() { 22 while (true) 23 The thread.yield (); 24 } 25 } 26 is 27 public class Daemons The Network { 28 public static void main (String [] args) throws Exception { 29 the Thread d = new new the Thread ( new new Daemon ()); 30 // thread is disposed d as a background thread, which is derived child threads (() Create a new Daemon) is also the default background thread 31 is d.setDaemon ( to true ); 32 d.start (); 33 is printnb ( "d.isDaemon () = "d.isDaemon + () +", " ); 34 is // Allow the daemon threads to 35 // finish their startup processes: 36 TimeUnit.SECONDS.sleep(1); 37 } 38 }
b. In the background thread does not execute finally clauses of the case will end its run () method
in other words, finally clause is not necessarily performed, if it is in a background thread, finally clause may not be executed is over
1 / ** 2 * background thread finally clauses may not be executed, the decision on when to end the non-background threads 3 * / 4 class ADaemon the implements Runnable { 5 public void RUN () { 6 the try { 7 Print ( "Starting ADaemon " ); . 8 TimeUnit.SECONDS.sleep (. 1 ); . 9 } the catch (InterruptedException E) { 10 Print (" Via the Exiting InterruptedException " ); . 11 } the finally { 12 is Print (" RUN This Should Always "?); 13 is } 14 } 15 } 16 . 17 public class DaemonsDontRunFinally { 18 is public static void main (String [] args) throws Exception { . 19 the Thread T = new new the Thread ( new new ADaemon ()); 20 is t.setDaemon ( to true ); 21 is t.start (); 22 is // if not delayed, probably will not perform Print ( "Starting ADaemon"); 23 is TimeUnit.SECONDS.sleep (. 1 ); 24 } 25 }