import threading
import time
import logging
logging.basicConfig(level=logging.INFO)
cups = []
lock = threading.Lock() #锁
def factory():
logging.info("杯子的数量:{}".format(cups))
def worker(n,lock):
while True:
lock.acquire() #请求到锁
count = len(cups)
logging.info("杯子数量:{}".format(count))
time.sleep(0.6)
if count >= n:
break
cups.append(1)
lock.release() # 释放锁
logging.info("{} make one".format(threading.current_thread().name))
logging.info("当前杯子数:{}".format(len(cups)))
for i in range(10):
t = threading.Thread(target=worker,args=(100,lock))
t.start()