1 Packagecom.xt.thinks21_2;2 3 ImportJava.util.concurrent.TimeUnit;4 5 /**6 * Background Threading Test7 * 8 * @authorAdministrator9 *Ten */ One Public classSimpledaemontestImplementsRunnable { A - @Override - Public voidrun () { the //TODO auto-generated Method Stub - while(true) { - Try { -TimeUnit.MILLISECONDS.sleep (100); +System.out.println (Thread.CurrentThread () + ":" + This); -}Catch(interruptedexception e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - } - } - - Public Static voidMain (string[] args) { - for(inti = 0; I < 10; i++) { inThread T =NewThread (Newsimpledaemontest ()); -T.setdaemon (true);//set to Background thread, if not set as non-background thread, will print thread information wirelessly to T.start (); + } -System.out.println ("All Deamon is start!"); the Try { *TimeUnit.MILLISECONDS.sleep (175);//175>100, every thread information is printed once $}Catch(interruptedexception e) {Panax Notoginseng //TODO auto-generated Catch block - e.printstacktrace (); the } + } A the}
A background thread differs from a method that is not a background thread: The background thread is actively setting Thread.setdeamon (TRUE)
When a non-background thread ends, the background thread forces the end
Java concurrency, background threads