How could I use Jython threads as they were Java threads?
Well, putting aside the fact that you are calling the start()
method from within the run()
method, which is a really bad idea because once a thread is started, if you attempt to start it again you will get a thread state exception, setting that aside, the problem is, most probably, that you are using Jython threading library and not Java's.
If you make sure to import as follows:
from java.lang import Thread, InterruptedException
Instead of
from threading import Thread, InterruptedException
And if you correct the problem I cited above, chances are that your code will run just fine.
Using the Jython's threading
library, you would need to change the code a bit, somewhat like:
from threading import Thread, InterruptedExceptionimport timeclass Cycle(Thread): canceled = False def run(self): while not self.canceled: try: print "Hello World" time.sleep(1) except InterruptedException: canceled = Trueif __name__ == '__main__': foo = Cycle() foo.start()
Which appears to work for me.