1 22 package org.jboss.util.deadlock; 23 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 27 34 public class DeadlockDetector 35 { 36 public static DeadlockDetector singleton = new DeadlockDetector(); 38 protected HashMap waiting = new HashMap (); 40 41 public void deadlockDetection(Object holder, Resource resource) 42 throws ApplicationDeadlockException 43 { 44 HashSet set = new HashSet (); 45 set.add(holder); 46 47 Object checkHolder = resource.getResourceHolder(); 48 49 synchronized (waiting) 50 { 51 addWaiting(holder, resource); 52 53 while (checkHolder != null) 54 { 55 Resource waitingFor = (Resource)waiting.get(checkHolder); 56 Object holding = null; 57 if (waitingFor != null) 58 { 59 holding = waitingFor.getResourceHolder(); 60 } 61 if (holding != null) 62 { 63 if (set.contains(holding)) 64 { 65 String msg = "Application deadlock detected, resource="+resource 67 +", holder="+holder+", waitingResource="+waitingFor 68 +", waitingResourceHolder="+holding; 69 throw new ApplicationDeadlockException(msg, true); 70 } 71 set.add(holding); 72 } 73 checkHolder = holding; 74 } 75 } 76 } 77 78 81 public void addWaiting(Object holder, Resource resource) 82 { 83 synchronized (waiting) 84 { 85 waiting.put(holder, resource); 86 } 87 } 88 89 92 public void removeWaiting(Object holder) 93 { 94 synchronized (waiting) 95 { 96 waiting.remove(holder); 97 } 98 } 99 100 } 101 | Popular Tags |