public class A extends Thread {
2. A() {
3. setDaemon(true);
4. }
5.
6. public void run() {
7. (new B()).start();
8. try {
9. Thread.sleep(60000);
10. } catch (InterruptedException x) {}
11. System.out.println(“A done”);
12. }
13.
14. class B extends Thread {
15. public void run() {
16. try {
17. Thread.sleep(60000);
18. } catch (InterruptedException x) {}
19. System.out.println(“B done”);
21. }
22. }
23.
24. public static void main(String[] args) {
25. (new A()).start();
26. }
27. }
A. A done
B. B done
C. A done
B done
D. B done
A done
E. There is no exception that the application will print anything.
F. The application outputs “A done” and “B done”, in no guaranteed order.为什么选E,什么也没有打印,这是为什么。
2. A() {
3. setDaemon(true);
4. }
5.
6. public void run() {
7. (new B()).start();
8. try {
9. Thread.sleep(60000);
10. } catch (InterruptedException x) {}
11. System.out.println(“A done”);
12. }
13.
14. class B extends Thread {
15. public void run() {
16. try {
17. Thread.sleep(60000);
18. } catch (InterruptedException x) {}
19. System.out.println(“B done”);
21. }
22. }
23.
24. public static void main(String[] args) {
25. (new A()).start();
26. }
27. }
A. A done
B. B done
C. A done
B done
D. B done
A done
E. There is no exception that the application will print anything.
F. The application outputs “A done” and “B done”, in no guaranteed order.为什么选E,什么也没有打印,这是为什么。
2: A() {
setDaemon(true);
}
A()的构造方法里并没有打印的操作,才不会打印.
有错误理解请大家指教.....
还有就是怎么给分。没见有给分的按钮
因为:
1.A为一守护线程,B在A的RUN()体内启动,它继续了A的守护特性,事实上,B也是一个守护线程.
2.当程序中所有非守护线程结束时,程序结束.本例中,A和B都还在SLEEP()过程中,程序已经结束,所以SLEEP()之后不会再有任何输出信息,也不会有异常.
3.如果把SHEEP()换成死循环,如下,也能得到一样的结果.
package test;public class A1 extends Thread {
A1() {
setDaemon(true);
} public void run() {
(new B()).start();
// try {
// Thread.currentThread().sleep(40);
// } catch (InterruptedException x) {
// }
boolean state = true;
while (state);
System.out.println("A done");
} class B extends Thread {
public void run() {
// try {
// Thread.currentThread().sleep(40);
// } catch (InterruptedException x) {
//
// }
boolean state = true;
while (state);
System.out.println("B done");
}
}
public static void main(String[] args) {
(new A1()).start();
}
}
public static void main(String[] args) {
(new A1()).start();
try {
Thread.currentThread().sleep(10000); //休眠更长时间
} catch (InterruptedException x) {
}
}5.第二点中关于"当程序中所有非守护线程结束时,程序结束",其实也有时间限制,也就是说,当程序中所有非守护线程结束一段时间后(一般虚拟机中为30毫秒),程序才会结束.所以,你想得到打印信息,也可以把睡眠时间设在30秒之内.