import threading,time
zhenglong=[]
zheng_lock=threading.Lock()
W=threading.Condition(lock=zheng_lock)
chi_lock=threading.Lock()
E=threading.Condition(lock=chi_lock)
def zheng(name):
while True:
E.acquire()
if zhenglong.__len__()==0:
for i in range(1,11):
zhenglong.append(i)
time.sleep(0.3)
print('{0}正在蒸第{1}个包子'.format(name,i))
print('包子蒸完了,唤醒吃货')
E.notify_all()
E.release()
W.acquire()
W.wait()
W.release()
threading._start_new_thread(zheng,('厨师',))
def chihuo(name):
while True:
E.acquire()
global zhenglong
if zhenglong.__len__()==0:
W.acquire()
W.notify_all()
W.release()
E.wait()
else:
s=zhenglong.pop()
print('{0}正在吃{1}个包子,剩余{2}个包子'.format(name,s,zhenglong.__len__()))
time.sleep(0.1)
E.release()
threading._start_new_thread(chihuo,('小刚',))
threading._start_new_thread(chihuo,('小明',))
threading._start_new_thread(chihuo,('小hong',))
input()
蒸包子
最新推荐文章于 2024-06-07 22:37:20 发布