package com.zjh.thread;import java.util.concurrent.*;public class SimpleDaemons implements Runnable{
public void run(){
Thread.currentThread().setDaemon(true);
while(true){
try {
TimeUnit.MILLISECONDS.sleep(500);
System.out.println(Thread.currentThread().getName() + " : 守护线程");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args){
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0;i <= 5; i++){
exec.execute(new SimpleDaemons());
}
exec.shutdown();
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//为何红色部分设置守护线程的时候会报错?
public void run(){
Thread.currentThread().setDaemon(true);
while(true){
try {
TimeUnit.MILLISECONDS.sleep(500);
System.out.println(Thread.currentThread().getName() + " : 守护线程");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args){
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0;i <= 5; i++){
exec.execute(new SimpleDaemons());
}
exec.shutdown();
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//为何红色部分设置守护线程的时候会报错?
是不是这个异常,一般是线程启动前就设置的