import threading,queue
import time,random
q = queue.Queue()
def Producer(name):
count =0
while count < 10:
print('making.........')
time.sleep(random.randrange(3))
q.put(count)
print('Product %s has produced %s baozi...' % (name,count))
count += 1
q.task_done()
print('ok.......')
def Consumer(name):
count = 0
while count < 10:
time.sleep(random.randrange(4))
if not q.empty():
data = q.get()
q.task_done
q.join()
print(data)
print('Comsumer %s has eat %s baozi...' % (name,data))
else:
print("-----------------没有包子了-----------------")
count += 1
p1 = threading.Thread(target = Producer,args = ('厨师',))
c1 = threading.Thread(target = Consumer,args = ('食客1',))
c2 = threading.Thread(target = Consumer,args = ('食客2',))
#c3 = threading.Thread(target = Consumer,args = ('食客3',))
p1.start()
c1.start()
c2.start()
#c3.start()
原文:https://www.cnblogs.com/hyxk/p/11289068.html