KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Stack JavaDoc;
19
20 public class ThreadStackImpl11 implements ThreadStack {
21     private Hashtable JavaDoc stacks = new Hashtable JavaDoc();
22     private Thread JavaDoc cached_thread;
23     private Stack JavaDoc cached_stack;
24     private int change_count = 0;
25     private static final int COLLECT_AT = 20000;
26     private static final int MIN_COLLECT_AT = 100;
27
28     public synchronized Stack JavaDoc getThreadStack() {
29         if (Thread.currentThread() != cached_thread) {
30             cached_thread = Thread.currentThread();
31             cached_stack = (Stack JavaDoc)stacks.get(cached_thread);
32             if (cached_stack == null) {
33                 cached_stack = new Stack JavaDoc();
34                 stacks.put(cached_thread, cached_stack);
35             }
36             change_count++;
37             // Collect more often if there are many threads, but not *too* often
38
int size = Math.max(1, stacks.size()); // should be >1 b/c always live threads, but...
39
if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) {
40                 Stack JavaDoc dead_stacks = new Stack JavaDoc();
41                 for (Enumeration JavaDoc e = stacks.keys(); e.hasMoreElements(); ) {
42                     Thread JavaDoc t = (Thread JavaDoc)e.nextElement();
43                     if (!t.isAlive()) dead_stacks.push(t);
44                 }
45                 for (Enumeration JavaDoc e = dead_stacks.elements(); e.hasMoreElements(); ) {
46                     Thread JavaDoc t = (Thread JavaDoc)e.nextElement();
47                     stacks.remove(t);
48                 }
49                 change_count = 0;
50             }
51         }
52         return cached_stack;
53     }
54
55 }
56
Popular Tags