import thread, time
def worker_thread(id):
print "Thread ID %d is now alive" %id
count = 1
while True:
print "Thread with ID %d has counter value %d" %(id, count)
time.sleep(2)
count += 1
for i in range(5):
thread.start_new_thread(worker_thread, (i,))
print "Main Thread Going For An Infinite Loop"
while True:
pass
OUTPUT in windows:
===================
Main Thread Going For An Infinite LoopThread ID 4 is now aliveThread ID 2 is now aliveThread ID 0 is now aliveThread ID 3 is now aliveThread ID 1 is now alive
Thread with ID 4 has counter value 1Thread with ID 2 has counter value 1Thread with ID 0 has counter value 1Thread with ID 3 has counter value 1Thread with ID 1 has counter value 1
Thread with ID 3 has counter value 2Thread with ID 2 has counter value 2Thread with ID 4 has counter value 2Thread with ID 0 has counter value 2Thread with ID 1 has counter value 2
Thread with ID 3 has counter value 3Thread with ID 0 has counter value 3Thread with ID 2 has counter value 3Thread with ID 4 has counter value 3Thread with ID 1 has counter value 3
Thread with ID 0 has counter value 4Thread with ID 3 has counter value 4Thread with ID 2 has counter value 4Thread with ID 4 has counter value 4Thread with ID 1 has counter value 4
def worker_thread(id):
print "Thread ID %d is now alive" %id
count = 1
while True:
print "Thread with ID %d has counter value %d" %(id, count)
time.sleep(2)
count += 1
for i in range(5):
thread.start_new_thread(worker_thread, (i,))
print "Main Thread Going For An Infinite Loop"
while True:
pass
OUTPUT in windows:
===================
Main Thread Going For An Infinite LoopThread ID 4 is now aliveThread ID 2 is now aliveThread ID 0 is now aliveThread ID 3 is now aliveThread ID 1 is now alive
Thread with ID 4 has counter value 1Thread with ID 2 has counter value 1Thread with ID 0 has counter value 1Thread with ID 3 has counter value 1Thread with ID 1 has counter value 1
Thread with ID 3 has counter value 2Thread with ID 2 has counter value 2Thread with ID 4 has counter value 2Thread with ID 0 has counter value 2Thread with ID 1 has counter value 2
Thread with ID 3 has counter value 3Thread with ID 0 has counter value 3Thread with ID 2 has counter value 3Thread with ID 4 has counter value 3Thread with ID 1 has counter value 3
Thread with ID 0 has counter value 4Thread with ID 3 has counter value 4Thread with ID 2 has counter value 4Thread with ID 4 has counter value 4Thread with ID 1 has counter value 4
No comments:
Post a Comment