/** * <p>标题: ThreadPriority</p> * <p> * 功能描述:线程守护,当主线程执行完后,其它线程(不管执行完没有)都自动退出。 * * </p> * <p>创建日期: 2016年3月4日 下午2:21:59</p> * <p>作者: lzd</p> * <p>版本: 1.0</p> */ public class ThreadGuard { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { int i = 0; while (i++ < 1000) { System.out.println(Thread.currentThread().getName() + "....................." + i); } } }); //只需要将后台线程的setDaemon设置为true即可 thread.setDaemon(true); thread.start(); Thread thread1 = new Thread(new Runnable() { @Override public void run() { int i = 0; while (i++ < 1000) { System.out.println(Thread.currentThread().getName() + "....................." + i); } } }); //只需要将后台线程的setDaemon设置为true即可 thread1.setDaemon(true); thread1.start(); int j = 0; while (j++ < 10) { System.out.println("MAIN....................." + j); } } }