KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > LockMonitor


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.monitor;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * Simple thread-safe POJO encapsulating locking stats.
28  *
29  * Turned this class to Serializable to be able to
30  * return copies of instances of this class over RMI.
31  *
32  * In this case it becomes detached from the EntityLockMonitor
33  * factory.
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
37  * @version $Revision: 37459 $
38  */

39 public class LockMonitor implements Serializable JavaDoc
40 {
41    // Private -------------------------------------------------------
42

43    /** @since 4.0.2 */
44    private static final long serialVersionUID = -6710878502772579272L;
45    
46    /* Lock Stats */
47    private long totalTime;
48    private long numContentions;
49    private long timeouts;
50    private long maxContenders;
51    private long currentContenders;
52    
53    /** Back reference to the non-Serializable LockMonitor factory */
54    private transient EntityLockMonitor parent;
55
56    // Constructors --------------------------------------------------
57

58    /**
59     * @param parent factory
60     */

61    public LockMonitor(EntityLockMonitor parent)
62    {
63       this.parent = parent;
64    }
65    
66    // Accessors -----------------------------------------------------
67

68    public synchronized long getTotalTime()
69    {
70       return totalTime;
71    }
72    
73    public synchronized long getNumContentions()
74    {
75       return numContentions;
76    }
77    
78    public synchronized long getTimeouts()
79    {
80       return timeouts;
81    }
82    
83    public synchronized long getMaxContenders()
84    {
85       return maxContenders;
86    }
87
88    public synchronized long getCurrentContenders()
89    {
90       return currentContenders;
91    }
92    
93    // Modifiers -----------------------------------------------------
94

95    /**
96     * Adjust the counters to indicate a contetion conditions.
97     *
98     * If the parent EntityLockMonitor has been initialized
99     * the total stats will be updated, as well.
100     */

101    public void contending()
102    {
103       synchronized(this)
104       {
105          ++numContentions;
106          ++currentContenders;
107          
108          if (currentContenders > maxContenders)
109          {
110             maxContenders = currentContenders;
111          }
112       }
113       
114       // Remark Ulf Schroeter: DO NOT include following call into the
115
// synchronization block because it will cause deadlocks between
116
// LockMonitor methods and EntityLockMonitor.clearMonitor() call!
117
if (parent != null)
118       {
119          parent.incrementContenders();
120       }
121    }
122
123    /**
124     * Adjust the counters to indicate that contention is over
125     *
126     * If the parent EntityLockMonitor has been initialized
127     * the total stats will be updated, too.
128     */

129    public void finishedContending(long time)
130    {
131       synchronized(this)
132       {
133          totalTime += time;
134          --currentContenders;
135       }
136
137       // Remark Ulf Schroeter: DO NOT include following call into the
138
// synchronization block because it will cause deadlocks between
139
// LockMonitor methods and EntityLockMonitor.clearMonitor() call!
140
if (parent != null)
141       {
142          parent.decrementContenders(time);
143       }
144    }
145    
146    /**
147     * Increase the timeouts on this lock
148     */

149    public void increaseTimeouts()
150    {
151       synchronized(this)
152       {
153          ++timeouts;
154       }
155    }
156    
157    /**
158     * Reset the counters.
159     *
160     * CurrentContenders stays unchanged and
161     * MaxCondenders is set to CurrentContenders
162     */

163    public void reset()
164    {
165       synchronized(this)
166       {
167          timeouts = 0;
168          totalTime = 0;
169          numContentions = 0;
170          // maxContenders always >= currentContenders
171
maxContenders = currentContenders;
172       }
173    }
174    
175    // Object overrides ----------------------------------------------
176

177    public String JavaDoc toString()
178    {
179       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(128);
180       
181       sbuf.append(super.toString())
182           .append("[ ")
183           .append("totalTime=").append(getTotalTime())
184           .append(", numContentions=").append(getNumContentions())
185           .append(", timeouts=").append(getTimeouts())
186           .append(", maxContenders=").append(getMaxContenders())
187           .append(", currentContenders=").append(getCurrentContenders())
188           .append(" ]");
189                 
190       return sbuf.toString();
191    }
192 }
Popular Tags