From: <pj...@us...> - 2008-08-08 22:07:27
|
Revision: 5111 http://jython.svn.sourceforge.net/jython/?rev=5111&view=rev Author: pjenvey Date: 2008-08-08 22:07:24 +0000 (Fri, 08 Aug 2008) Log Message: ----------- o another fix to make 'currentThread() is currentThread()' from thread.start_new_thread threads. use a WeakHashMap because weakrefs of javainstances ref the wrapper, not the actual java object o custom toString for FunctionThread Modified Paths: -------------- branches/asm/Lib/threading.py branches/asm/src/org/python/core/FunctionThread.java Modified: branches/asm/Lib/threading.py =================================================================== --- branches/asm/Lib/threading.py 2008-08-08 19:30:38 UTC (rev 5110) +++ branches/asm/Lib/threading.py 2008-08-08 22:07:24 UTC (rev 5111) @@ -1,3 +1,4 @@ +from java.util import Collections, WeakHashMap from java.util.concurrent import Semaphore, CyclicBarrier from java.util.concurrent.locks import ReentrantLock from thread import _newFunctionThread @@ -160,6 +161,8 @@ class JavaThread(object): def __init__(self, thread): self._thread = thread + _jthread_to_pythread[thread] = self + _threads[thread.getId()] = self def __repr__(self): _thread = self._thread @@ -209,10 +212,12 @@ # relies on the fact that this is a CHM _threads = weakref.WeakValueDictionary() _active = _threads -_jthread_to_pythread = weakref.WeakKeyDictionary() +_jthread_to_pythread = Collections.synchronizedMap(WeakHashMap()) class Thread(JavaThread): def __init__(self, group=None, target=None, name=None, args=None, kwargs=None): + _thread = self._create_thread() + JavaThread.__init__(self, _thread) if args is None: args = () if kwargs is None: @@ -220,11 +225,8 @@ self._target = target self._args = args self._kwargs = kwargs - self._thread = _thread = self._create_thread() if name: self._thread.setName(name) - _jthread_to_pythread[_thread] = self - _threads[_thread.getId()] = self def _create_thread(self): return _newFunctionThread(self.__bootstrap, ()) @@ -314,10 +316,11 @@ return None def currentThread(): - try: - return _jthread_to_pythread[java.lang.Thread.currentThread()] - except KeyError: - return JavaThread(java.lang.Thread.currentThread()) + jthread = java.lang.Thread.currentThread() + pythread = _jthread_to_pythread[jthread] + if pythread is None: + pythread = JavaThread(jthread) + return pythread def activeCount(): return len(_threads) Modified: branches/asm/src/org/python/core/FunctionThread.java =================================================================== --- branches/asm/src/org/python/core/FunctionThread.java 2008-08-08 19:30:38 UTC (rev 5110) +++ branches/asm/src/org/python/core/FunctionThread.java 2008-08-08 22:07:24 UTC (rev 5111) @@ -28,4 +28,15 @@ Py.printException(exc); } } + + @Override + public String toString() { + ThreadGroup group = getThreadGroup(); + if (group != null) { + return String.format("FunctionThread[%s,%s,%s]", getName(), getPriority(), + group.getName()); + } else { + return String.format("FunctionThread[%s,%s]", getName(), getPriority()); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |