//把守护线程放在线程开启之前,因为线程起动起来了就不可以设置为守护线程.
//异常是java.lang.IllegalThreadStateException,非法的线程状态.
public class Demo03 {
public static void main(String[] args) {
Thread t1 = new Thread(){
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println("A");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread(new Runnable(){
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("B");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
t2.setDaemon(true); //把守护线程放在线程开启之前,因为线程起动起来了就不可以设置为守护线程.
t1.start();
t2.start();
}
}
|