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.   

    1. public class Threads 1 {
    2. intx=0;
    3. public class Runner implements Runnable {
    4. public void run() {
    5. int current = 0;
    6. for(int=i=0;i<4;i++){
    7. current = x;
    8. System.out.print(current + “, “);
    9. x = current + 2;
    10. }
    11. }
    12. }
    13.
    14. public static void main(String[] args) {
    15. new Threads1().go();
    16. }
    17.
    18. public void go() {
    19. Runnable r1 = new Runner();
    20. new Thread(r1).start();
    21. new Thread(r1 ).start();
    22. }
    23. }
    Which two are possible results? (Choose two.)
    A. 0, 2, 4, 4, 6, 8, 10, 6,
    B. 0, 2, 4, 6, 8, 10, 2, 4,
    C. 0, 2, 4, 6, 8, 10, 12, 14,
    D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
    E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
    Answer: AC
    看不懂答案为什么是AC~~高人帮忙分析一下~~
      

  2.   

     这个问题你可以先去掉后面一个 new Thread(r1).start();输出的是0,2,4,6
    第二个 new Thread(r1).start();中r1是第一个r1中执行run()方法后的引用
      

  3.   

    第一道题目是个很好的题目额,由于线程的不可预知性,使得线程执行顺序无法人为控制,而这里提供了一种人为控制的办法 delay();首先肯定进入主函数,没有疑问,
    其次,启动线程1,也没问题,这时候在线程1中delay(6000)延时了6秒钟,而主函数一共才延时3秒,这里,你可能会有个问题,为什么线程2不运行,这是由于线程1中锁住了resource对象,那么使得线程2无法运行,剩下的应该都不是问题了
      

  4.   


    高人都研究算法去了,他们不会理这些的
    那我就答答吧
    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. } 首先先打印出 startMain 信息
     然后执行 thread1()的一个线程对象,这个线程占用resource对象的锁,其他线程就 不允许进入,直到它完成,释放锁。
     所以再打印出start1
     然后此线程要去睡觉6000毫秒,注意这时候它没有释放锁
     休眠1000毫秒后,线程t2也开启,但是它没有获得resource的对象锁,所以只能挂起
     等待thread1() 的那个线程完成  此时该线程还要睡觉5000毫秒
     然后又继续休眠1000毫秒,在将t2线程中断
     再休眠1000毫秒
     然后打印出EndMain ,此时thread1()那个线程还在睡觉 还要睡觉3000毫秒
     3000毫秒后,此线程醒过来了,输出 End1,并且释放resource对象锁
     然后挂起的线程t2开始执行
     打印出 START2
     此线程也要休眠2000毫秒,但是t2已经被中断了,所以抛出InterruptedException 
     中断异常。打印错误流 Error
     然后再执行打印出 end2 
    1. public class Threads 1 {
    2. int x=0;
    3. public class Runner implements Runnable {
    4. public void run() {
    5. int current = 0;
    6. for(int i=0;i<4;i++){
    7. current = x;
    8. System.out.print(current + “, “);
    9. x = current + 2;
    10. }
    11. }
    12. }
    13.
    14. public static void main(String[] args) {
    15. new Threads1().go();
    16. }
    17.
    18. public void go() {
    19. Runnable r1 = new Runner();
    20. new Thread(r1).start();
    21. new Thread(r1 ).start();
    22. }
    23. } 这个其实是看new Thread(r1).start(); 线程1
                 new Thread(r1 ).start();线程2
     这两个线程的执行顺序。
     只能是输出8个值。
     new thread(r1).start()开始执行后,它总会输出 0,2,4,6
     但是 new thread(r1) 就不确定从哪里开始啊,当执行完上线程1的0,线程2就从current=0开始赋值,可能结果0,0,2,2,4,4,6,6 等等
     当执行完线程1的输出2,线程2 就从current=2 开始赋值 
     可能结果 0,2,2,4,4,6,6,8 等等……
     当执行完线程1的输出4,线程2 就从current=4开始赋值
     可能结果  0,2,4,4,6,6,8,10 等等
     …………
     …………
     所以一看,就晓得AC是可能结果啊 执行顺序由操作系统的时间片决定,它的调度就不需要了解了