生產者消費者模型

import threading,time,queue

q=queue.Queue(maxsize=10)

def Producer(name):
    count=1
    while True:
        q.put("骨頭%s"%count)
        print("生成了骨頭:",count)
        count +=1 #count=count +1
        time.sleep(0.5)

def Consumer(name):
    #while q.qsize()>0:
    while True:
        print("[%s]取到[%s]並且吃了他..."%(name,q.get()))
        time.sleep(1)

p=threading.Thread(target=Producer,args=("Lei",))
c=threading.Thread(target=Consumer,args=("Han×××",))
c2=threading.Thread(target=Consumer,args=("Bily",))

p.start()
c.start()
c2.start()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章