- java中多线程,守护模式
zookeeper中监听事件就是守护模式
守护模式:主线程死了,子线程也跟着结束
非守护模式:主线程死了,子线程继续
public class TreadDemoGw {
public static void main(String[] args) {
System.out.println("主线程开始启动....");Thread thread = new Thread(new Runnable() {
public void run() {
System.out.println("子线程开始启动....");
while(true){
System.out.println("催眠大法!博主是个大帅哥!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
//子线程 守护模式?true=主线程死了,子线程也跟着结果:false:主线程死了,子线程继续// thread.setDaemon(true);
thread.start();
System.out.println("主线已启动子线程-催眠大法....");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("主线程执行10秒后-催眠结束!");
}
}