## example 12.1, page no. 211¶

In [18]:
"""
note: the output will differ from that given in textbook as Threading in Python differs from that in J
"""

for i in range(1, 6):
print "From Thread A: i = ", i
print "Exit from A"

for i in range(1, 6):
print "From Thread B: i = ", i
print "Exit from B"

for i in range(1, 6):
print "From Thread C: i = ", i
print "Exit from C"


From Thread C: i =  1
From Thread C: i =  2
From Thread C: i =  3
From Thread C: i =  4
From Thread C: i =  5
Exit from C
From Thread A: i =  1
From Thread A: i =  2
From Thread A: i =  3
From Thread A: i =  4
From Thread A: i =  5
Exit from A
From Thread B: i =  1
From Thread B: i =  2
From Thread B: i =  3
From Thread B: i =  4
From Thread B: i =  5
Exit from B


## example 12.2, page no. 217¶

In [2]:
"""
there is no yield or stop function for threads in Python. Demonstrating just sleep() function
"""

import time

for i in range(1, 6):
print "From Thread A: i = ", i
print "Exit from A"

for i in range(1, 6):
print "From Thread B: i = ", i
print "Exit from B"

for i in range(1, 6):
print "From Thread C: i = ", i
time.sleep(0.5)
print "Exit from C"

time.sleep(0.5)
time.sleep(0.5)
time.sleep(0.5)

From Thread A: i =  1
From Thread A: i =  2
From Thread A: i =  3
From Thread A: i =  4
From Thread A: i =  5
Exit from A
From Thread B: i =  1
From Thread B: i =  2
From Thread B: i =  3
From Thread B: i =  4
From Thread B: i =  5
Exit from B
From Thread C: i =  1

In [ ]:
#There is no way you can set priority for a thread in Python. Hence, example 12.3 is avoided


## example 12.4, page no. 225¶

In [69]:
"""
There is no runnable interface in Python. Will use normal threading instead
"""

def run(self):
for i in range(1, 11):

runnable = X()
runnable.run()

End of main thread