# CS 450; Thread example 5 # Create and join threads in python # version 2014-01-26 11:26 AM # import threading import time from threading import Thread def main(): print("Start then join each thread\n") for i in range(5): # Create a thread that sleeps for 6-i sec thd = Thread(target=say_hello, \ args=(6-i,), \ name="mythread_" + str(i)) thd.start() print('starting {}'.format(thd.name)) thd.join() def say_hello(sleep_seconds): myname = threading.current_thread().name print('hello from {}'.format(myname)) time.sleep(sleep_seconds) print('goodbye from {}'.format(myname)) main() # run the main program # Sample output: # # hello from mythread_0 # starting mythread_0 # goodbye from mythread_0 # hello from mythread_1 # starting mythread_1 # goodbye from mythread_1 # hello from mythread_2 # starting mythread_2 # goodbye from mythread_2 # hello from mythread_3 # starting mythread_3 # goodbye from mythread_3 # hello from mythread_4 # starting mythread_4 # goodbye from mythread_4