import threading import time def print_time(threadName, delay, iterations): start = int(time.time()) for i in range(0,iterations): time.sleep(delay) seconds_elapsed = str(int(time.time()) - start) print ('{} {}'.format(seconds_elapsed, threadName)) t = threading.Thread(target=print_time, args=('Fizz', 3, 3)).start() t = threading.Thread(target=print_time, args=('Buzz', 5, 3)).start() t = threading.Thread(target=print_time, args=('Counter', 1, 10)).start()
输出
1 Counter 2 Counter 3 Fizz 3 Counter 4 Counter 5 Buzz 5 Counter 6 Fizz 6 Counter 7 Counter 8 Counter 9 Fizz 9 Counter 10 Buzz 10 Counter 15 Buzz [Program finished]