public class SyncTest{  public static void main(String[] args)  {  final StringBuffer s1= new StringBuffer();  final StringBuffer s2= new StringBuffer();  new Thread ()   {    public void run() {      synchronized(s1) {          s2.append(“A”);           synchronized(s2) {    s2.append(“B”);      System.out.print(s1);        System.out.print(s2);       }      }    }    }.start();  new Thread() {    public void run() {      synchronized(s2) {        s2.append(“C”);        synchronized(s1) {         s1.append(“D”);          System.out.print(s2);          System.out.print(s1);         }       }       }     }.start();    }  } Which two statements are true? (Choose Two) A. The program prints “ABBCAD”B. The program prints “CDDACB”C. The program prints “ADCBADBC”D. The output is a non-deterministic point because of a possible deadlock conditionE. The output is dependent on the threading model of the system the program is running on.

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【Fenglee2008】截止到2008-06-22 21:56:06的历史汇总数据(不包括此帖):
    发帖数:23                 发帖分:476                
    结贴数:20                 结贴分:455                
    未结数:3                  未结分:21                 
    结贴率:86.96 %            结分率:95.59 %            
    楼主加油
      

  2.   

    我觉得这题选择DE,首先,代码中出现了死锁,很有可能当第一个线程中执行到synchronized(s2)代码块时,第二个线程已经将s2对象锁住,同样第二个线程中执行到synchronized(s1)代码块时,第一个线程已经将s1对象锁住,这就造成了死锁.
    但是由于程序在不同的平台上运行,涉及到的时间片轮换问题不同,多以也有可能,线程2起动之前,线程1已经执行完毕.这是我个人的观点,参考一下,不一定对噢,呵呵~~
      

  3.   

    同意楼上观点顺便问下StringBuffer 的append不受final控制吗?
    还有其他的类似情况吗?
      

  4.   

    ==============
    这里的final只是限制 引用s1不能引用其他StringBuffer对象,而不是限制它所引用的对象不能被修改!