KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > deadlock > DeadlockDetector


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.deadlock;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 /**
28  * Created by IntelliJ IDEA.
29  * User: wburke
30  * Date: Aug 21, 2003
31  * Time: 2:10:46 PM
32  * To change this template use Options | File Templates.
33  */

34 public class DeadlockDetector
35 {
36    // TODO Maybe this should be an MBean in the future
37
public static DeadlockDetector singleton = new DeadlockDetector();
38    // This following is for deadlock detection
39
protected HashMap JavaDoc waiting = new HashMap JavaDoc();
40
41    public void deadlockDetection(Object JavaDoc holder, Resource resource)
42            throws ApplicationDeadlockException
43    {
44       HashSet JavaDoc set = new HashSet JavaDoc();
45       set.add(holder);
46
47       Object JavaDoc 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 JavaDoc holding = null;
57             if (waitingFor != null)
58             {
59                holding = waitingFor.getResourceHolder();
60             }
61             if (holding != null)
62             {
63                if (set.contains(holding))
64                {
65                   // removeWaiting should be cleaned up in acquire
66
String JavaDoc 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    /**
79     * Add a transaction waiting for a lock
80     */

81    public void addWaiting(Object JavaDoc holder, Resource resource)
82    {
83       synchronized (waiting)
84       {
85          waiting.put(holder, resource);
86       }
87    }
88
89    /**
90     * Remove a transaction waiting for a lock
91     */

92    public void removeWaiting(Object JavaDoc holder)
93    {
94       synchronized (waiting)
95       {
96          waiting.remove(holder);
97       }
98    }
99
100 }
101
Popular Tags