多线程相关:按序打印、交替打印FooBar、交替打印字符串

LeetCode刷题 专栏收录该内容
26 篇文章 0 订阅

一、按序打印

在这里插入图片描述

from threading import Lock
class Foo:
    def __init__(self):
        self.firstJobDone = Lock()
        self.secondJobDone = Lock()
        self.firstJobDone.acquire()
        self.secondJobDone.acquire()

    # 要求执行顺序是first、second、third
    def first(self, printFirst: 'Callable[[], None]') -> None:
        printFirst()
        self.firstJobDone.release()

    def second(self, printSecond: 'Callable[[], None]') -> None:
        with self.firstJobDone: # with语法:自动获取且用完之后自动释放锁
            printSecond()
            self.secondJobDone.release()


    def third(self, printThird: 'Callable[[], None]') -> None:
        with self.secondJobDone:
            printThird()

二、交替打印FooBar

在这里插入图片描述

'''
生产者与消费者模型(信号量解法)
acquire(),表示p操作,加1
release(),表示v操作,减1
'''
class FooBar:
    def __init__(self, n):
        self.n = n
        self.p = threading.Semaphore(1)
        self.v = threading.Semaphore(0)

    def foo(self, printFoo: 'Callable[[], None]') -> None:
        for i in range(self.n):
            self.p.acquire()
            printFoo()
            self.v.release()


    def bar(self, printBar: 'Callable[[], None]') -> None:
            for i in range(self.n):
                self.v.acquire()
                printBar()
                self.p.release()

三、交替打印字符串(不懂)

在这里插入图片描述
在这里插入图片描述

from threading import Semaphore

class FizzBuzz:
    def __init__(self, n: int):
        self.n = n
        self.sem_fizz = Semaphore(0)
        self.sem_buzz = Semaphore(0)
        self.sem_fibu = Semaphore(0)
        self.sem_num = Semaphore(1)

    # printFizz() outputs "fizz"
    def fizz(self, printFizz: 'Callable[[], None]') -> None:
        for i in range(1, self.n+1):
            if i % 3 == 0 and i % 5 != 0:
                self.sem_fizz.acquire()
                printFizz()
                self.sem_num.release()

    # printBuzz() outputs "buzz"
    def buzz(self, printBuzz: 'Callable[[], None]') -> None:
        for i in range(1, self.n+1):
            if i % 3 != 0 and i % 5 == 0:
                self.sem_buzz.acquire()
                printBuzz()
                self.sem_num.release()


    # printFizzBuzz() outputs "fizzbuzz"
    def fizzbuzz(self, printFizzBuzz: 'Callable[[], None]') -> None:
        for i in range(1, self.n+1):
            if i % 3 == 0 and i % 5 == 0:
                self.sem_fibu.acquire()
                printFizzBuzz()
                self.sem_num.release()


    # printNumber(x) outputs "x", where x is an integer.
    def number(self, printNumber: 'Callable[[int], None]') -> None:
        for i in range(1, self.n+1):
            self.sem_num.acquire()
            if i % 3 == 0 and i % 5 == 0:
                self.sem_fibu.release()
            elif i % 3 == 0:
                self.sem_fizz.release()
            elif i % 5 == 0:
                self.sem_buzz.release()
            else:
                printNumber(i)
                self.sem_num.release()
  • 0
    点赞
  • 1
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值