1 import sys 2 import time 3 import threading 4 import queue 5 6 def producer(): 7 for x in range(20): 8 time.sleep(1) 9 q.put(x) 10 print("生产%d个苹果" % x) 11 def cost(): 12 for x in range(20): 13 time.sleep(1.5) 14 print("消耗%d个苹果" % q.get()) 15 16 if __name__ == '__main__': 17 q = queue.Queue(maxsize=5) 18 t1 = threading.Thread(target=producer) 19 t2 = threading.Thread(target=cost) 20 t1.start() 21 t2.start()