KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > runtime > internal > cflowstack > ThreadCounterImpl11


1 /* *******************************************************************
2  * Copyright (c) 2004 IBM Corporation
3  *
4  * All rights reserved.
5  * This program and the accompanying materials are made available
6  * under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * Andy Clement initial implementation
12  * Copied from bits of original CFlowStack
13  * ******************************************************************/

14 package org.aspectj.runtime.internal.cflowstack;
15
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 public class ThreadCounterImpl11 implements ThreadCounter {
23     private Hashtable JavaDoc counters = new Hashtable JavaDoc();
24     private Thread JavaDoc 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             // Collect more often if there are many threads, but not *too* often
45
int size = Math.max(1, counters.size()); // should be >1 b/c always live threads, but...
46
if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) {
47                 List JavaDoc dead_stacks = new ArrayList JavaDoc();
48                 for (Enumeration JavaDoc e = counters.keys(); e.hasMoreElements(); ) {
49                     Thread JavaDoc t = (Thread JavaDoc)e.nextElement();
50                     if (!t.isAlive()) dead_stacks.add(t);
51                 }
52                 for (Iterator JavaDoc e = dead_stacks.iterator(); e.hasNext(); ) {
53                     Thread JavaDoc t = (Thread JavaDoc)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