python的多线程——抢面包

from  threading import  Thread
import threading
import time


# 全局变量
sum = 500
# 为了防止厨师的打印比较的混乱,使用厨师的全局锁
# mutex = threading.Lock()

# 为了防止顾客的打印比较的混乱,使用顾客的全局锁
# mutex1 = threading.Lock()
start = time.time() # 获取现在时间

class Consumer(Thread):
    mutex1 = threading.Lock()
    count = 0

    username = ""


    def run(self) -> None:
        # global  mutex1
        global  sum
        while True:
            end = time.time()
            if end - start >= 300:
                SystemExit
            self.mutex1.acquire()  # 当执行该代码后,锁定当前人的打印的程序,其他人的打印暂且先排队,优先打印当前的程序
            if sum > 0:
                self.count = self.count + 1
                sum = sum  - 1
                print(self.username,"--------------抢了1个面包!还剩",sum,"个面包!!已经买了",self.count,"个面包!")
                time.sleep(0.1)
            else:
                print(self.username,"-------------面包不足,请等待3秒钟!",end="")
                for i in range(3):
                    time.sleep(1)
                    print(".",end="")
            self.mutex1.release() # 释放这个打印锁,让排队的其他人继续执行

class Chief(Thread):
    mutex = threading.Lock()
    username = ""
    count = 0

    def run(self) -> None:
        global  sum
        while True:
            self.mutex.acquire()  # 当执行该代码后,锁定当前人的打印的程序,其他人的打印暂且先排队,优先打印当前的程序
            if sum < 500:
                sum = sum + 1
                self.count = self.count + 1
                print(self.username , "-------------造了一个面包!还剩",sum,"个面包!已经造了",self.count,"个面包!")

            else:
                print(self.username,"-----------面包篮子已满!请等待6秒.")
                for i in range(6):
                    time.sleep(1)
                    print(".",end="")
            self.mutex.release()  # 释放这个打印锁,让排队的其他人继续执行

c1 = Consumer()
c2 = Consumer()
c3 = Consumer()
c4 = Consumer()
c5 = Consumer()
p1 = Chief()
p2 = Chief()
p3 = Chief()
p4 = Chief()
p5 = Chief()
c1.username = "张三"
c2.username = "李四"
c3.username = "王五"
c4.username = "赵六"
c5.username = "田七"
p1.username = "厨师1"
p2.username = "厨师2"
p3.username = "厨师3"
p4.username = "厨师4"
p5.username = "厨师5"

c1.start()
p1.start()
c2.start()
# p2.start()
c3.start()
# p3.start()
c4.start()
# p4.start()
c5.start()
# p5.start()


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值