1 8 9 package com.sleepycat.je.latch; 10 11 import java.util.Collections ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import java.util.WeakHashMap ; 18 19 22 class LatchTable { 23 24 private String tableName; 25 private Map latchesByThread; 26 27 LatchTable(String tableName) { 28 29 this.tableName = tableName; 30 latchesByThread = Collections.synchronizedMap(new WeakHashMap ()); 31 } 32 33 36 boolean noteLatch(Object latch) 37 throws LatchException { 38 39 Thread cur = Thread.currentThread(); 40 41 Set threadLatches = (Set ) latchesByThread.get(cur); 42 if (threadLatches == null) { 43 threadLatches = new HashSet (); 44 latchesByThread.put(cur, threadLatches); 45 } 46 threadLatches.add(latch); 47 return true; 48 } 49 50 54 boolean unNoteLatch(Object latch, String name) { 55 56 Thread cur = Thread.currentThread(); 57 58 Set threadLatches = (Set ) latchesByThread.get(cur); 59 60 if (threadLatches == null) { 61 return false; 62 } else { 63 return threadLatches.remove(latch); 64 } 65 } 66 67 70 int countLatchesHeld() { 71 72 Thread cur = Thread.currentThread(); 73 Set threadLatches = (Set ) latchesByThread.get(cur); 74 if (threadLatches != null) { 75 return threadLatches.size(); 76 } else { 77 return 0; 78 } 79 } 80 81 String latchesHeldToString() { 82 83 Thread cur = Thread.currentThread(); 84 Set threadLatches = (Set ) latchesByThread.get(cur); 85 StringBuffer sb = new StringBuffer (); 86 if (threadLatches != null) { 87 Iterator 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 102 String getNameString(String name) { 103 104 StringBuffer sb = new StringBuffer (tableName); 105 if (name != null) { 106 sb.append("(").append(name).append(")"); 107 } 108 return sb.toString(); 109 } 110 111 114 String toString(String name, Object owner, List waiters, int startIndex) { 115 116 117 StringBuffer sb = new StringBuffer (); 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 |