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,什么也没有打印,这是为什么。

解决方案 »

  1.   

    1:的确没有exception
    2: A() {
    setDaemon(true);
    }
    A()的构造方法里并没有打印的操作,才不会打印.
    有错误理解请大家指教.....
      

  2.   

    setDaemon是什么意思。构造方法里没有可是run里有,线程执行的run方法啊。
    还有就是怎么给分。没见有给分的按钮
      

  3.   

    E是个that句,不是可能打印任何情况,否定句变成anything,什么也不打印。我理解的意思是因为没有异常所以不打印。打印的语句没有写在异常里,就是有异常也不打印啊。
      

  4.   

    选E,不会有任何内容输出.
    因为:
    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();
    }
    }
      

  5.   

    4.如果想打出您想要的结果,可在MAIN中加入如下代码:
    public static void main(String[] args) {
    (new A1()).start();
    try {
    Thread.currentThread().sleep(10000); //休眠更长时间
    } catch (InterruptedException x) {
    }
    }5.第二点中关于"当程序中所有非守护线程结束时,程序结束",其实也有时间限制,也就是说,当程序中所有非守护线程结束一段时间后(一般虚拟机中为30毫秒),程序才会结束.所以,你想得到打印信息,也可以把睡眠时间设在30秒之内.
      

  6.   

    刚开始以为B类为内类所以不打印。就在类A外创建了一个类C。被main调用,C可以被打印。就以为是内类的关系。结果把C放到A中调用也不能打印了。现在知道与内外类无关,只要被守护线程调用到的方法所属的类也就具有了守护线程的特性。