条件Condition

from threading import Condition,Thread
import time


def func(con,i):
    con.acquire()
    con.wait()
    print("第%s个线程执行了" % i)
    con.release()


con = Condition()  # 先实例化一个对象
for i in range(10):  # 创建10个子线程
    t = Thread(target=func,args=(con,i))
    t.start()

while True:
    num = int(input(">>>:"))
    con.acquire()
    con.notify(num) # 给wait发送int个信号,会传递给int个wait,让int个线程正常执行
    con.release()
    time.sleep(1)

 

posted @ 2018-10-28 09:12  Nope!!!!  阅读(140)  评论(0编辑  收藏  举报