1 14 package org.aspectj.runtime.internal.cflowstack; 15 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Enumeration ; 20 import java.util.Hashtable ; 21 22 public class ThreadCounterImpl11 implements ThreadCounter { 23 private Hashtable counters = new Hashtable (); 24 private Thread cached_thread; 25 private Counter cached_counter; 26 27 private int change_count = 0; 28 private static final int COLLECT_AT = 20000; 29 private static final int MIN_COLLECT_AT = 100; 30 31 static class Counter { 32 protected int value = 0; 33 } 34 35 private synchronized Counter getThreadCounter() { 36 if (Thread.currentThread() != cached_thread) { 37 cached_thread = Thread.currentThread(); 38 cached_counter = (Counter)counters.get(cached_thread); 39 if (cached_counter == null) { 40 cached_counter = new Counter(); 41 counters.put(cached_thread, cached_counter); 42 } 43 change_count++; 44 int size = Math.max(1, counters.size()); if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) { 47 List dead_stacks = new ArrayList (); 48 for (Enumeration e = counters.keys(); e.hasMoreElements(); ) { 49 Thread t = (Thread )e.nextElement(); 50 if (!t.isAlive()) dead_stacks.add(t); 51 } 52 for (Iterator e = dead_stacks.iterator(); e.hasNext(); ) { 53 Thread t = (Thread )e.next(); 54 counters.remove(t); 55 } 56 change_count = 0; 57 } 58 } 59 return cached_counter; 60 } 61 62 public void inc() { 63 getThreadCounter().value++; 64 } 65 66 public void dec() { 67 getThreadCounter().value--; 68 } 69 70 public boolean isNotZero() { 71 return getThreadCounter().value!=0; 72 } 73 74 } 75 | Popular Tags |