Duvida questão 130 TestKiller

Question 130 Click the Exhibit button. 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.
Answer: ABE

Valeu Tomaz! Consegui capturar!

sobre ser letra A

//r1 = 10 . r2 = 10 doTransfer(r1, r2, 5); //r1 = 5, r2 = 15 doTransfer(r2, r1, 2); //r1 = 7, r2 = 13 doTransfer(r1, r2, 1); //r1 = 6, r2 = 14

sobre letra B …
passa so roda o 1° doTransfer, ou seja… r1=5, e r2=15 … e logo depois imprime, antes de executar os outros doTransfer

sobre letra E …

doTransfer(r1, r2, 5); //se aqui ele so xegar ao primeiro synch onde ele sincroniza r1 doTransfer(r2, r1, 2); //ai aqui ele synch r2 no start, e depois pede bloquei de r1, que esta com outra thread //ai o 1° do tranfer tenta pedir o bloquei de r2, que esta com a outra thread... e fica as 2 no empasse... deadlock...


Sobre a letra C…
a letra C nunca acorre em nenhuma das combinações possiveis … talvez vc tenha axado q o segundo doTransfer de esse resultado, mas não da