synchronized ( Expression ) Block The type of Expression must be a reference type, or a compile-time error occurs. A synchronized statement is executed by first evaluating the Expression. If evaluation of the Expression completes abruptly for some reason, then the synchronized statement completes abruptly for the same reason. Otherwise, if the value of the Expression is null, a NullPointerException is thrown. Otherwise, let the non-null value of the Expression be V. The executing thread locks the lock associated with V. Then the Block is executed. If execution of the Block completes normally, then the lock is unlocked and the synchronized statement completes normally. If execution of the Block completes abruptly for any reason, then the lock is unlocked and the synchronized statement then completes abruptly for the same reason
应该说这是一种对代码块进行同步的机制,Expression 掌握着锁

解决方案 »

  1.   

    1,FINAL变量所指向的地址不能修改,也就是说它指到A对象后不能再改到B对象,但A对象本身可以修改.
    2,线程启动的先后或是先后的多少对结果有影响并且可能发生死锁
      

  2.   

    2、synchronized(s1)
          { 
            s1.append("A"); ////会被中断处
          synchronized(s2)
          { 
            s2.append("B"); 
          System.out.println("11:" + s1 + " "); 
          System.out.println("12:" + s2 + " "); 
           } 
           } 
    在这里完全有可能被中断,? synchronized(s1)就是说:当线称得到对象监视器s1 时才会进入下面的代码,得到对象监视器s2后才会进行
    s2.append("B"); 
          System.out.println("11:" + s1 + " "); 
          System.out.println("12:" + s2 + " "); 
    结束了这个代码块后会施放对象监视器s2,接着施放对象监视器s1.
      

  3.   

    1.
    a final field has been initialized, it always contains the same value. If a final field holds a reference to an object, then the state of the object may be changed by operations on the object, but the field will always refer to the same object. 
    如果这里将StringBuffer 换为String 就不行了。