1 Reply Latest reply: Apr 30, 2009 7:13 AM by 800345 RSS

    deadlock

    807588
      Please explain how the deadlock happens below.
      1. public class DeadlockRisk {
      2. private static class Resource {
      3. public int value;
      4. }
      5. private Resource resourceA = new Resource();
      6. private Resource resourceB = new Resource();
      7. public int read() {
      8. synchronized(resourceA) { // May deadlock here
      9. synchronized(resourceB) {
      10. return resourceB.value + resourceA.value;
      11. }
      12. }
      13. }
      14.
      15. public void write(int a, int b) {
      16. synchronized(resourceB) { // May deadlock here
      17. synchronized(resourceA) {
      18. resourceA.value = a;
      19. resourceB.value = b;
      20. }
      21. }
      22. }
      23. }