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 version = System.getProperty("java.version"); 11 if (version.compareTo("1.2") >= 0) { 12 try { 13 Class c = Class.forName( 14 "org.python.core.ThreadStateMapping2"); 15 return (ThreadStateMapping) c.newInstance(); 16 } catch (Throwable t) { } 17 } 18 } 19 return new ThreadStateMapping(); 20 } 21 22 23 private static java.util.Hashtable threads; 26 private static ThreadState cachedThreadState; 27 28 private static int additionCounter = 0; 35 private static final int MAX_ADDITIONS = 25; 37 38 39 public ThreadState getThreadState(PySystemState newSystemState) { 40 Thread 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 (); 48 49 ts = (ThreadState)threads.get(t); 50 if (ts == null) { 51 if (newSystemState == null) { 52 Py.writeDebug("threadstate", "no current system state"); 53 newSystemState = Py.defaultSystemState; 55 } 56 ts = new ThreadState(t, newSystemState); 57 threads.put(t, ts); 59 additionCounter++; 62 if (additionCounter > MAX_ADDITIONS) { 63 cleanupThreadTable(); 64 additionCounter = 0; 65 } 66 } 67 cachedThreadState = ts; 68 return ts; 70 } 71 72 77 private void cleanupThreadTable() { 78 for (java.util.Enumeration e = threads.keys(); e.hasMoreElements();) { 80 try { 81 Object 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 exc) { 91 } 93 } 94 } 95 } 96 | Popular Tags |