(1)为什么是DE:Given:
1. public class Threads2 implements Runnable {
2.
3. public void nun() {
4. System.out.println(”run.”);
5. throw new RuntimeException(”Problem”);
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println(”End of method.”);
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
Answer: DE
(2)答案为什么是G?Click the Exhibit button.
Given:
1. public class TwoThreads {
2
3. private static Object resource = new Object();
4.
5. private static void delay(long n) {
6. try { Thread.sleep(n); }
7. catch (Exception e) { System.out.print(”Error “); }
8. }
9
10. public static void main(String[] args) {
11. System.out.print(”StartMain “);
12. new Thread1().start();
13. delay(1000);
14. Thread t2 = new Thread2();
15. t2.start();
16. delay(1000);
17. t2.interrupt
18. delay(1000);
19. System.out.print(”EndMain “);
20. }
21.
22. static class Thread 1 extends Thread {
23. public void run() {
24. synchronized (resource) {
25. System.out.print(”Startl “);
26. delay(6000);
27. System.out.print(”End1 “);
28. }
29. }
30. }
31.
32. static class Thread2 extends Thread {
33. public void run() {
34. synchronized (resource) {
35. System.out.print(”Start2 “);
36. delay(2000);
37. System.out.print(”End2 “);
38. }
39. }
40. }
41. }
Assume that sleep(n) executes in exactly m milliseconds, and all other
code executes in an insignificant amount of time. What is the output if
the main() method is run?
A. Compilation fails.
B. Deadlock occurs.
C. StartMain Start1 Error EndMain End1
D. StartMain Start1 EndMain End1 Start2 End2
E. StartMain Start1 Error Start2 EndMain End2 End1
F. StartMain Start1 Start2 Error End2 EndMain End1
G. StartMain Start1 EndMain End1 Start2 Error End2
Answer: G

解决方案 »

  1.   


    main方法本身也是一个线程,当同时存在多个线程的时候,它们会竞争cpu,所以它们的执行顺序是没有保障的,就是说,哪个线程得到了cpu,哪个就可以执行,但是也不一定一直到线程结束,因为在执行的过程就有可能其他的线程夺去了.
      

  2.   


    有点头绪了
    我总觉得main方法要等其他方法和线程执行完了,它排在最后。
      

  3.   

    "我总觉得main方法要等其他方法和线程执行完了,它排在最后。"
    这样认为就不对了第二题主要是
    synchronized (resource) 
    这里锁定了,当Thread1先执行的时候,Thread2就只能等Thread1执行完后才能执行,因为他们锁定在同一对象上,反之亦然不理解的话google,baidu搜搜
      

  4.   

    SCJP中的题?
      

  5.   

    第2题先格式化一下:public class TwoThreads { private static Object resource = new Object(); private static void delay(long n) {
    try {
    Thread.sleep(n);
    } catch (Exception e) {
    System.out.print("Error ");
    }
    } public static void main(String[] args) {
    System.out.print("StartMain ");
    new Thread1().start();
    delay(1000);
    Thread t2 = new Thread2();
    t2.start();
    delay(1000);
    t2.interrupt();
    delay(1000);
    System.out.print("EndMain ");
    } static class Thread1 extends Thread { public void run() {
    synchronized (resource) {
    System.out.print("Startl ");
    delay(6000);
    System.out.print("End1 ");
    }
    } } static class Thread2 extends Thread { public void run() {
    synchronized (resource) {
    System.out.print("Start2 ");
    delay(2000);
    System.out.print("End2 ");
    }
    }
    }}
      

  6.   

    谢谢9楼!
    谢谢sagezk家里在装修没网,每次都是跑网吧来的
    没软件
    谢谢!
      

  7.   

    第2题回答:程序运行开始,首先main线程启动进入main方法执行打印出StartMain,然后main线程创建Thread1并使其处于可运行状态(通过new Thread1().start();),此时Thread1并不会马上到CPU上运行,接下来delay(1000);使当前线程即main线程sleep1000毫秒使Thread1获得运行机会并到CPU上运行。Thread1首先获取resource同步锁,然后打印Startl,接下来delay(6000)使当前线程Thread1进入长达6000毫秒的休眠。在Thread1睡眠1000毫秒后main线程delay(1000)时间到开始继续运行,创建Thread2并使其处于可运行状态,然后main会delay(1000)进入休眠此时Thread2得到运行机会到CPU上开始运行。但到synchronized (resource)时发现resource同步锁已被Thread1取走,所以Thread2被阻塞。1000毫秒后main苏醒开始继续运行,t2.interrupt();将Thread2设置上中断标志,而后delay(1000),此时Thread2等待同步锁被阻塞Thread1睡眠6000毫秒还剩下4000毫秒,1000毫秒后main苏醒(此时Thread1还需睡3000毫秒)然后打印EndMain至此main线程生命结束。3000毫秒后Thread1苏醒进入可运行状态,而此时三个线程中main结束Thread2被阻塞,所以Thread1会马上获得运行机会继续运行打印End1,而后Thread1释放resource的同步锁后结束生命,此时只有Thread2还活着。当Thread2发现resource同步锁已被Thread1释放后Thread2获得同步锁,继续运行打印Start2,当运行到delay(2000)时当前线程Thread2要进入休眠状态,而此时Thread2的中断标志已被设置,所以delay(2000)执行到Thread.sleep(n);时会抛出InterruptedException异常,异常被捕获后打印出Error,而后delay(2000)方法马上返回(而没进入2000毫秒的休眠)继续执行System.out.print("End2 ");打印出End2后释放resource同步锁。至此Thread2的run方法执行结束,线程Thread2生命结束,3个构成Java进程的线程都执行结束程序最终正常退出,剩下的就只有线程们的运行痕迹了 StartMain Startl EndMain End1 Start2 Error End2,经测试实际的运行结果也大多如此