__author__ = "Alex Li"
import threading, time
import queue
'''
在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。
该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
'''
q = queue.Queue(maxsize=10)
def producer(name):
count = 1
while True:
q.put("骨头%s" % count)
print("生产了骨头", count)
count += 1
time.sleep(0.1)
def consumer(name):
# while q.qsize() > 0:
while True:
print("[%s] 取到[%s] 并且吃了它..." % (name, q.get()))
time.sleep(1)
p = threading.Thread(target=producer, args=("Alex",))
# 去队列取值,不会乱序,平衡生产线程和消费线程的工作能力
c = threading.Thread(target=consumer, args=("ChengRonghua",))
c1 = threading.Thread(target=consumer, args=("王森",))
p.start()
c.start()
c1.start()