今天的一个关于Thread的scjp题目,一个网友除了这一道其他全对了。

luzhiac 2002-01-18 02:47:16
Q1
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. s1.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) {
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.
我拿着程序运行了一下,输出"ABBCAD",即A,但是要求选择两个,不懂。
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Patrick_DK 2002-01-18
  • 打赏
  • 举报
回复
D是没有问题的

还有E:这个程序的输出结果要依赖于这个程序所运行在的系统上的线程调度模式.
这句话应该是对的.

问题是这个程序有可能会输出A和B的啊,有可能的选项应该也没有错
~~~~~~
pensincun 2002-01-18
  • 打赏
  • 举报
回复
这是SUN的SCJP的题库中的真题!!!
正确的答案应该是:D、E!!!
这是个典型的死锁deadlock的Thread类的考题!
你多运行几次就知道正确的结果了!
注意:synchronized方法的运用!!!要按照一定的顺序获得LOCK,也必须按照相应的顺序释放LOCK!
我2次SCJP碰到了此题!
Patrick_DK 2002-01-18
  • 打赏
  • 举报
回复
D是肯定对的.还有一个我不明白是哪个?

关于这道题,详细的讨论请往

http://www.csdn.net/Expert/topic/486/486177.shtm

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧