KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > latch > LatchTable


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LatchTable.java,v 1.10 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.latch;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.WeakHashMap JavaDoc;
18
19 /**
20  * Table of latches by thread for debugging.
21  */

22 class LatchTable {
23
24     private String JavaDoc tableName;
25     private Map JavaDoc latchesByThread;
26
27     LatchTable(String JavaDoc tableName) {
28         
29         this.tableName = tableName;
30         latchesByThread = Collections.synchronizedMap(new WeakHashMap JavaDoc());
31     }
32
33     /**
34      * Only call under the assert system. This records latching by thread.
35      */

36     boolean noteLatch(Object JavaDoc latch)
37     throws LatchException {
38
39         Thread JavaDoc cur = Thread.currentThread();
40
41         Set JavaDoc threadLatches = (Set JavaDoc) latchesByThread.get(cur);
42         if (threadLatches == null) {
43             threadLatches = new HashSet JavaDoc();
44             latchesByThread.put(cur, threadLatches);
45         }
46         threadLatches.add(latch);
47         return true;
48     }
49
50     /**
51      * Only call under the assert system. This records latching by thread.
52      * @return true if unnoted successfully.
53      */

54     boolean unNoteLatch(Object JavaDoc latch, String JavaDoc name) {
55
56         Thread JavaDoc cur = Thread.currentThread();
57
58         Set JavaDoc threadLatches = (Set JavaDoc) latchesByThread.get(cur);
59         
60         if (threadLatches == null) {
61             return false;
62         } else {
63             return threadLatches.remove(latch);
64         }
65     }
66
67     /**
68      * Only call under the assert system. This counts held latches.
69      */

70     int countLatchesHeld() {
71
72         Thread JavaDoc cur = Thread.currentThread();
73         Set JavaDoc threadLatches = (Set JavaDoc) latchesByThread.get(cur);
74         if (threadLatches != null) {
75             return threadLatches.size();
76         } else {
77             return 0;
78         }
79     }
80
81     String JavaDoc latchesHeldToString() {
82
83         Thread JavaDoc cur = Thread.currentThread();
84         Set JavaDoc threadLatches = (Set JavaDoc) latchesByThread.get(cur);
85         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
86         if (threadLatches != null) {
87             Iterator JavaDoc i = threadLatches.iterator();
88             while (i.hasNext()) {
89                 sb.append(i.next()).append('\n');
90             }
91         }
92         return sb.toString();
93     }
94
95     void clearNotes() {
96         latchesByThread.clear();
97     }
98
99     /**
100      * For concocting exception messages.
101      */

102     String JavaDoc getNameString(String JavaDoc name) {
103
104     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(tableName);
105     if (name != null) {
106         sb.append("(").append(name).append(")");
107     }
108         return sb.toString();
109     }
110
111     /**
112      * Formats a latch owner and waiters.
113      */

114     String JavaDoc toString(String JavaDoc name, Object JavaDoc owner, List JavaDoc waiters, int startIndex) {
115
116         /* Assume the caller does synchronization. */
117     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
118     sb.append("<LATCH ");
119         if (name != null) {
120             sb.append("[name: ").append(name).append("] ");
121         }
122         sb.append("[owner: ").append(owner).append("]");
123         if (waiters != null && waiters.size() > startIndex) {
124             sb.append(" [waiters: ");
125             for (int i = startIndex; i < waiters.size(); i++) {
126                 sb.append(waiters.get(i)).append(" ");
127             }
128             sb.append("]");
129         }
130     sb.append(">");
131     return sb.toString();
132     }
133 }
134
135
Popular Tags