Java多线程之后台线程
将线程设置成后台线程Daemons 主线程结果后,后台线程将自动结果。
package wzh.test; import java.util.concurrent.TimeUnit; class SimpleDaemons implements Runnable{ @Override public void run() { try { while (true) { TimeUnit.MILLISECONDS.sleep(100); System.out.println(Thread.currentThread().getId()+" "+this); } } catch (Exception e) { System.out.println("sleep() interrupted"); } } } public class SimpleDaemonsMain{ public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { Thread daemon=new Thread(new SimpleDaemons()); daemon.setDaemon(true); daemon.start(); } System.out.println(Thread.currentThread().getId()+"All daemons started"); TimeUnit.MICROSECONDS.sleep(175234231); } }