讨论/题目交流/多线程1195.交替打印字符串报错/
多线程1195.交替打印字符串报错

一直报错,显示Fizz Seamphore object is not callable,请问a哪里错了呀!!!

import threading 
class FizzBuzz:
    def __init__(self, n: int):
        self.n = n
        self.fizz=threading.Semaphore(0)
        self.fizzbuzz=threading.Semaphore(0)
        self.buzz=threading.Semaphore(0)
        self.num=threading.Semaphore(1)

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

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

    # printFizzBuzz() outputs "fizzbuzzu
    def fizzbuzz(self, printFizzBuzz: 'Callable[[], None]') -> None:
        for i in range(n):
            if i%3==0 and i%5==0:
                self.fizzbuzz.acquire()
                printFizzBuzz()
                self.num.release()
    	
    # printNumber(x) outputs "x", where x is an integer.
    def number(self, printNumber: 'Callable[[int], None]') -> None:
        for i in range(n):
            self.num.acquire()
            if i%3==0 and i%5==0:
                self.fizzbuzz.release()
            elif i%3==0:
                self.fizz.release()
            elif i%5==0:
                self.buzz.release()
            else:
                printNumber(i)
                self.num.release()
共 1 个回复

解决了,变量个和函数重名了,太傻了