线程同步Condition
1、10个线程,打印1-10000,要求,threading1 只打印1...
import time
import threading
from threading import Thread, Condition
num = 1
condition = Condition()
condition1 = Condition()
condition2 = Condition()
condition3 = Condition()
condition4 = Condition()
condition5 = Condition()
condition6 = Condition()
condition7 = Condition()
condition8 = Condition()
condition9 = Condition()
def thread(cond1,cond2,end):
global num
while True:
with cond1:
cond1.wait()
print(threading.currentThread().name, num)
num += 1
with cond2:
cond2.notify()
if num>end:
return
Thread(target=thread,name="threading1",args=(condition,condition1,9991)).start()
Thread(target=thread,name="threading2",args=(condition1,condition2,9992)).start()
Thread(target=thread,name="threading3",args=(condition2,condition3,9993)).start()
Thread(target=thread,name="threading4",args=(condition3,condition4,9994)).start()
Thread(target=thread,name="threading5",args=(condition4,condition5,9995)).start()
Thread(target=thread,name="threading6",args=(condition5,condition6,9996)).start()
Thread(target=thread,name="threading7",args=(condition6,condition7,9997)).start()
Thread(target=thread,name="threading8",args=(condition7,condition8,9998)).start()
Thread(target=thread,name="threading9",args=(condition8,condition9,9999)).start()
Thread(target=thread,name="threading10",args=(condition9,condition,10000)).start()
with condition:
condition.notify()
永远不要高估自己