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.
楼主【Fenglee2008】截止到2008-06-22 21:56:06的历史汇总数据(不包括此帖):
发帖数:23 发帖分:476
结贴数:20 结贴分:455
未结数:3 未结分:21
结贴率:86.96 % 结分率:95.59 %
楼主加油
但是由于程序在不同的平台上运行,涉及到的时间片轮换问题不同,多以也有可能,线程2起动之前,线程1已经执行完毕.这是我个人的观点,参考一下,不一定对噢,呵呵~~
还有其他的类似情况吗?
这里的final只是限制 引用s1不能引用其他StringBuffer对象,而不是限制它所引用的对象不能被修改!