Topic: Synchronized的用法 |
Print this page |
1.Synchronized的用法 | Copy to clipboard |
Posted by: jin3shan Posted on: 2006-09-08 11:43 1. public class SyncTest{ 2. public static void main(String[] args) { 3. final StringBuffer s1= new StringBuffer(); 4. final StringBuffer s2= new StringBuffer(); 5. new Thread () { 6. public void run() { 7. synchronized(s1) { 8. s2.append(“A”); 9. synchronized(s2) { 10. s2.append(“B”); 11. System.out.print(s1); 12. System.out.print(s2); 13. } 14. } 15. } 16. }.start(); 17. new Thread() { 18. public void run() { 19. synchronized(s2) { // synchronized 20. s2.append(“C”); 21. synchronized(s1) { 22. s1.append(“D”); 23. System.out.print(s2); 24. System.out.print(s1); 25. } 26. } 27. } 28. }.start(); 29. } 30. } 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 condition. E. The output is dependent on the threading model of the system the program is running on. 正确答案: D,B 注解: |
2.Re:Synchronized的用法 [Re: jin3shan] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2006-09-10 08:26 Your program is not compilable! No answer is correct!!!! |
3.Re:Synchronized的用法 [Re: jin3shan] | Copy to clipboard |
Posted by: jin3shan Posted on: 2006-09-11 19:14 不是吧.我运行都行啊.再说D答案怎么也是正确的呀 |
4.Re:Synchronized的用法 [Re: jin3shan] | Copy to clipboard |
Posted by: cxp108 Posted on: 2006-09-12 09:48 D答案正确。 这个程序随时都有可能出现死锁的状态。 因为,尽管线程1的start()调用得比线程2的要早,但是并不代表这两个线程进入run() 函数的先后顺序。 也就是说,线程2先进入run()的可能性完全是有的,而且程序控制权在线程之间的交替, 完全可能导致以下情况: 线程1将s1锁死,正在等待s2解锁,在它获得s2的锁之前,它不会释放s1的锁 线程2将s2锁死,正在等待s1解锁,在它获得s1的锁之前,它不会释放s2的锁 两个线程相互等待对方释放锁,结果就是死锁..... |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |