Daemon线程
public class ThreadA {public static void main(String[] args) {
System.out.println("ThreadA执行:");
Thread threadB = new Thread(new Runnable() {
public void run() {
try {
System.out.println("ThreadB开始:");
for (int i = 0; i < 5; i++ ) {
Thread.sleep(1000);
System.out.println("ThreadB执行..");
}
System.out.println("ThreadB即将结束..");
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
);
threadB.start();
threadB.setDaemon(true);//设置为Daemon线程
System.out.println("ThreadA执行..");
}
}
C:\Users\Administrator\Desktop\classes>java ThreadA
ThreadA执行:
ThreadB开始:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1275)
at ThreadA.main(ThreadA.java:121)
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB执行..
ThreadB即将结束..
为什么没有执行System.out.println("ThreadA执行..");这个语句,求指导