请指教 经典试题 选什么 答对就给分最好有点解释哦
时间:2008-05-10 21:48:37
来源:论坛整理 作者: 编辑:chinaitzhe
10. public class Transfers {
11. public static void main(String[] args) throws Exception {
12. Record r1 = new Record();
13. Record r2 = new Record();
14. doTransfer(r1, r2, 5);
15. doTransfer(r2, r1, 2);
16. doTransfer(r1, r2, 1);
17. // print the result
18. System.out.println("rl = " r1.get() ", r2=" r2.get());
19. }
20. private static void doTransfer(
21. final Record a, final Record b, final int amount) {
22. Thread t = new Thread() {
23. public void run() {
24. new Clerk().transfer(a, b, amount);
25. }
26. };
27. t.start();
28. }
29. }
30. class Clerk {
31. public synchronized void transfer(Record a, Record b, int amount){
32. synchronized (a) {
33. synchronized (b) {
34. a.add(-amount);
35. b.add(amount);
36. }
37. }
38. }
39. }
40. class Record {
41.int num=10;
42. public int get() { return num; }
43. public void add(int n) { num = num n; }
44. }
If Transfers.main() is run, which three are true? (Choose three.)
A. The output may be "r1 = 6, r2 = 14".
B. The output may be "r1 = 5, r2 = 15".
C. The output may be "r1 = 8, r2 = 12".
D. The code may run (and complete) with no output.
E. The code may deadlock (without completing) with no output.
F. M IllegalStateException or InterruptedException may be thrown at
runtime.
网友回复:可能性太多了
A D E
除此之外还有许多选项之外的答案 线程执行的顺序性是不确定的
就说deadlock是发生在a b两同步块之间
网友回复:和答案对不住哦
网友回复:ABC 还有其它的输出 10,10;7,13
就是因为线程运行顺序的不确定性 哈哈
网友回复:A, B, E
关键字:指教,经典,试题,答对,给分,最好,
上一篇:这段代码是不是恶意代码
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面