"""
note: the output will differ from that given in textbook as Threading in Python differs from that in J
"""
def threadA():
for i in range(1, 6):
print "From Thread A: i = ", i
print "Exit from A"
def threadB():
for i in range(1, 6):
print "From Thread B: i = ", i
print "Exit from B"
def threadC():
for i in range(1, 6):
print "From Thread C: i = ", i
print "Exit from C"
thread1 = Thread(target=threadA, args=())
thread2 = Thread(target=threadB, args=())
thread3 = Thread(target=threadC, args=())
thread1.start()
thread2.start()
thread3.start()
"""
there is no yield or stop function for threads in Python. Demonstrating just sleep() function
"""
from threading import Thread
import time
def threadA():
for i in range(1, 6):
print "From Thread A: i = ", i
print "Exit from A"
def threadB():
for i in range(1, 6):
print "From Thread B: i = ", i
print "Exit from B"
def threadC():
for i in range(1, 6):
print "From Thread C: i = ", i
time.sleep(0.5)
print "Exit from C"
thread1 = Thread(target=threadA, args=())
thread2 = Thread(target=threadB, args=())
thread3 = Thread(target=threadC, args=())
thread1.start()
time.sleep(0.5)
thread2.start()
time.sleep(0.5)
thread3.start()
time.sleep(0.5)
#There is no way you can set priority for a thread in Python. Hence, example 12.3 is avoided
"""
There is no runnable interface in Python. Will use normal threading instead
"""
import threading
class X(threading.Thread):
def run(self):
for i in range(1, 11):
print "ThreadX: ", i
print "End of ThreadX"
runnable = X()
threadx = Thread()
threadx.start()
print "End of main thread"
runnable.run()