多线程实例
1 import threading 2 import time 3 4 5 def eat(): 6 eatTime = time.time() 7 for i in range(30): 8 print(‘count = {}‘.format(count)) 9 time.sleep(1) 10 print ‘eat something in {}‘.format(eatTime) 11 return eatTime 12 13 def playGame(): 14 playTime = time.time() 15 16 for i in range(30): 17 global count 18 count += 1 19 time.sleep(1) 20 print ‘play Game in {}‘.format(playTime) 21 return playTime 22 count = 0 23 theadEat = threading.Thread(target=eat) 24 theatPlay = threading.Thread(target=playGame) 25 theadEat.start() 26 theatPlay.start() 27 eatT = theadEat.join() 28 playT = theatPlay.join() 29 30 # eatT = eat() 31 # playT = playGame() 32 33 print("over in {}!".format(time.time()))
多线程代码
1 count = 0 2 count = 1 3 count = 2 4 count = 3 5 count = 4 6 count = 6 7 count = 6 8 count = 7 9 count = 8 10 count = 9 11 count = 11 12 count = 11 13 count = 13 14 count = 14 15 count = 15 16 count = 16 17 count = 17 18 count = 18 19 count = 19 20 count = 20 21 count = 21 22 count = 22 23 count = 23 24 count = 24 25 count = 25 26 count = 26 27 count = 27 28 count = 28 29 count = 29 30 count = 30 31 play Game in 1551163617.11 32 eat something in 1551163617.11 33 over in 1551163647.13!
运行结果
原文地址:https://www.cnblogs.com/xsmile/p/10437313.html
时间: 2024-10-18 05:32:36