import threading
import queue
import random
import time
qq = queue.Queue(4) #实例化一个队列,因为是一个进程的线程,所以共资源
#重写线程类
class produce(threading.Thread):
def __init__(self,myqueue):
super().__init__()
self.q = myqueue
def run(self):
while True:
item = random.randint(0,55)
self.q.put(item)
print("生产了",item) class cus(threading.Thread):
def __init__(self,myqueue):
super().__init__()
self.q = myqueue
def run(self):
while True:
item = self.q.get()
print("消费了",item) t1 = produce(qq)
t1.start()
t2 = cus(qq)
t2.start()
图例: