import time import threading def music(fun): for i in range(2): print(‘music‘) time.sleep(2) print(‘end the music‘) def mover(func): for i in range(2): print(‘movers‘) time.sleep(8) print(‘end the movers‘) # IO form --> sleep() blocking Threads = [] t1 = threading.Thread(target=music,args=(‘qil‘,)) t2 = threading.Thread(target=mover,args=(‘xiu‘,)) Threads.append(t1) Threads.append(t2) begin = time.time() for t in Threads: t.start() t.join() # available
#t.join() # unavailable # t2.start() # t1.join() # t2.join() end = time.time() print(‘combination‘,end-begin)
THE OUTPUT:
# t.join() is available
music movers end the music music end the music end the movers movers end the movers combination 16.012058973312378
#t.join() isn‘t available
music
movers
combination 0.0
end the music
music
end the movers
movers
end the music
end the movers
原文:https://www.cnblogs.com/pie-o/p/10726672.html