KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > ThreadStateMapping


1
2 package org.python.core;
3
4 class ThreadStateMapping {
5     private static boolean checkedJava2 = false;
6
7     public static ThreadStateMapping makeMapping() {
8         if (!checkedJava2) {
9             checkedJava2 = true;
10             String JavaDoc version = System.getProperty("java.version");
11             if (version.compareTo("1.2") >= 0) {
12                 try {
13                     Class JavaDoc c = Class.forName(
14                                   "org.python.core.ThreadStateMapping2");
15                     return (ThreadStateMapping) c.newInstance();
16                 } catch (Throwable JavaDoc t) { }
17             }
18         }
19         return new ThreadStateMapping();
20     }
21
22
23     // There are hacks that could improve performance slightly in the
24
// interim, but I'd rather wait for the right solution.
25
private static java.util.Hashtable JavaDoc threads;
26     private static ThreadState cachedThreadState;
27
28     // Mechanism provided by Drew Morrissey and approved by JimH which
29
// occasionally cleans up dead threads, preventing the hashtable from
30
// leaking memory when many threads are used (e.g. in an embedded
31
// application).
32
//
33
// counter of additions to the threads table
34
private static int additionCounter = 0;
35     // maximum number of thread additions before cleanup is triggered
36
private static final int MAX_ADDITIONS = 25;
37
38
39     public ThreadState getThreadState(PySystemState newSystemState) {
40         Thread JavaDoc t = Thread.currentThread();
41         ThreadState ts = cachedThreadState;
42         if (ts != null && ts.thread == t) {
43             return ts;
44         }
45
46         if (threads == null)
47             threads = new java.util.Hashtable JavaDoc();
48
49         ts = (ThreadState)threads.get(t);
50         if (ts == null) {
51             if (newSystemState == null) {
52                 Py.writeDebug("threadstate", "no current system state");
53                 //t.dumpStack();
54
newSystemState = Py.defaultSystemState;
55             }
56             ts = new ThreadState(t, newSystemState);
57             //System.err.println("new ts: "+ts+", "+ts.systemState);
58
threads.put(t, ts);
59             // increase the counter each time a thread reference is added
60
// to the table
61
additionCounter++;
62             if (additionCounter > MAX_ADDITIONS) {
63                 cleanupThreadTable();
64                 additionCounter = 0;
65             }
66         }
67         cachedThreadState = ts;
68         //System.err.println("returning ts: "+ts+", "+ts.systemState);
69
return ts;
70     }
71
72     /**
73      * Enumerates through the thread table looking for dead thread
74      * references and removes them. Called internally by
75      * getThreadState(PySystemState).
76      */

77     private void cleanupThreadTable() {
78         // loop through thread table removing dead thread references
79
for (java.util.Enumeration JavaDoc e = threads.keys(); e.hasMoreElements();) {
80             try {
81                 Object JavaDoc key = e.nextElement();
82                 ThreadState tempThreadState = (ThreadState)threads.get(key);
83                 if ((tempThreadState != null) &&
84                     (tempThreadState.thread != null) &&
85                     !tempThreadState.thread.isAlive())
86                 {
87                     threads.remove(key);
88                 }
89             }
90             catch (ClassCastException JavaDoc exc) {
91                 // TBD: we should throw some type of exception here
92
}
93         }
94     }
95 }
96
Popular Tags