Well, putting aside the fact that you call the start()
method from the run()
method, which is very bad, because as soon as the thread is started, if you try to start it again, you will get a thread state exception, otherwise the problem it is most likely that you are using the Jython thread library, not Java.
If you are sure to import as shown below:
from java.lang import Thread, InterruptedException
Instead
from threading import Thread, InterruptedException
And if you fix the problem mentioned above, most likely your code will work fine.
Using the Jython threading
library, you need to modify the code a bit, for example:
from threading import Thread, InterruptedException import time class Cycle(Thread): canceled = False def run(self): while not self.canceled: try: print "Hello World" time.sleep(1) except InterruptedException: canceled = True if __name__ == '__main__': foo = Cycle() foo.start()
Which seems to work for me.
source share