[python]代码库
import threading
import random
import time
gMoney = 0
gCondition = threading.Condition()
gTimes = 0
class Producer(threading.Thread):
def run(self):
global gMoney
global gTimes
while True:
gCondition.acquire()
if gTimes > 10:
gCondition.release()
break
money = random.randint(0, 100)
gMoney += money
gTimes += 1
print('%s赚了%d元钱,总资产为%d元钱' % (threading.current_thread().name, money, gMoney))
gCondition.notify_all()
gCondition.release()
time.sleep(1)
class Consumer(threading.Thread):
def run(self):
global gMoney
while True:
gCondition.acquire()
money = random.randint(0, 100)
while gMoney < money:
if gTimes > 10:
print('%s想消费%d元钱,但是只有%d元钱,并且生产者已经不再生产钱了!' % (threading.current_thread().name, money, gMoney))
gCondition.release()
return None
print('%s想消费%d元钱,但是只有%d元钱,消费失败!' % (threading.current_thread().name, money, gMoney))
gCondition.wait()
gMoney -= money
print('%s消费了%s元钱,还剩%d元钱。' % (threading.current_thread().name, money, gMoney))
gCondition.release()
time.sleep(1)
def main():
for i in range(5):
th = Producer(name='生产者%d号' % i)
th.start()
for i in range(5):
th = Consumer(name='消费者%d号' % i)
th.start()
if __name__ == '__main__':
main()