From: Ype K. <yk...@xs...> - 2002-11-12 21:58:04
|
Eric, On Tuesday 12 November 2002 12:13, Eric Jain wrote: > Is there any way to stop an Interpreter that is executing code from wit= hin > a Java program? I am aware of InteractiveInterpreter.interrupt(), but t= hat > only seems to work for waking up blocked or sleeping threads. Having th= e > Interpreter regularly check whether its thread was interrupted would be= a > very useful feature, as far as I am concerned! A java thread running a jython interpreter is no more and no less than a = java=20 thread. Interrupting such a thread is just as difficult as interrupting a java=20 thread, because the jython interpreter does not check for thread interrup= ted=20 status. You might extend the interpreter to do this check, but then it wo= uld=20 run slower. Since the interpreter can call any java code that does not=20 necessarily check the interrupted status, such a check would not be a str= ong=20 guarantee anyway. You can use the deprecated way to stop a java thread because it is the=20 simplest; see the docs of java.lang.Thread on why you might want to avoid= =20 this. This way is used in Console.py in the swing examples in the demo=20 directory of your jython installation. A cleaner way is to write a function that checks for some status and call= it=20 at the beginning of every possibly unbound loop, ie. as the first stateme= nt=20 in each deepest level 'while' statement. Sometimes it makes sense to use = it=20 in deepest level 'for' statements, too. The status check function can simply throw an exception, which you may or= may=20 not catch. Anyway, make sure to use try/finally where necessary to release locks and= /or=20 send ending signals to other waiting threads. Have fun, Ype |