Fizz Buzz Multithreaded Problem
Description
LeetCode Problem 1195.
You have the four functions:
printFizzthat prints the word"fizz"to the console,printBuzzthat prints the word"buzz"to the console,printFizzBuzzthat prints the word"fizzbuzz"to the console, andprintNumberthat prints a given integer to the console.
You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:
- Thread A: calls
fizz()that should output the word"fizz". - Thread B: calls
buzz()that should output the word"buzz". - Thread C: calls
fizzbuzz()that should output the word"fizzbuzz". - Thread D: calls
number()that should only output the integers.
Modify the given class to output the series [1, 2, "fizz", 4, "buzz", ...] where the i^th token (1-indexed) of the series is:
"fizzbuzz"ifiis divisible by3and5,"fizz"ifiis divisible by3and not5,"buzz"ifiis divisible by5and not3, oriifiis not divisible by3or5.
Implement the FizzBuzz class:
FizzBuzz(int n)Initializes the object with the numbernthat represents the length of the sequence that should be printed.void fizz(printFizz)CallsprintFizzto output"fizz".void buzz(printBuzz)CallsprintBuzzto output"buzz".void fizzbuzz(printFizzBuzz)CallsprintFizzBuzzto output"fizzbuzz".void number(printNumber)Callsprintnumberto output the numbers.
Example 1:
Input: n = 15
Output: [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
Example 2:
Input: n = 5
Output: [1,2,"fizz",4,"buzz"]
Constraints:
1 <= n <= 50
Solution
Approach: number thread as dispatcher
Four threads each print one kind of token, but the tokens must appear in the order 1..n. Let the number thread walk i = 1..n and decide whose turn it is:
- Each of
fizz,buzzandfizzbuzzhas its own semaphore, initially 0, and a sharednumberTurnsemaphore starts at 1. - For each
i,numberacquiresnumberTurn. Ifiis a multiple of 15 it releases thefizzbuzzsemaphore, else a multiple of 3 releasesfizz, else a multiple of 5 releasesbuzz. Otherwise it printsiitself and releasesnumberTurnfor the next value. - The other three threads know in advance how many times they will print:
fizzfor multiples of 3 that are not multiples of 5,buzzfor multiples of 5 that are not multiples of 3,fizzbuzzfor multiples of 15. Each waits on its semaphore, prints, and returns the turn throughnumberTurn.
The multiples of 15 must be excluded from the fizz and buzz loops. Otherwise those threads would wait for a permit that never comes and the program would hang.
Complexity: O(n) time overall, O(1) extra space.
from threading import Semaphore
class FizzBuzz:
def __init__(self, n: int):
self.n = n
self.number_turn = Semaphore(1)
self.fizz_turn = Semaphore(0)
self.buzz_turn = Semaphore(0)
self.fizzbuzz_turn = Semaphore(0)
# printFizz() outputs "fizz"
def fizz(self, printFizz: 'Callable[[], None]') -> None:
for i in range(3, self.n + 1, 3):
if i % 5 != 0:
self.fizz_turn.acquire()
printFizz()
self.number_turn.release()
# printBuzz() outputs "buzz"
def buzz(self, printBuzz: 'Callable[[], None]') -> None:
for i in range(5, self.n + 1, 5):
if i % 3 != 0:
self.buzz_turn.acquire()
printBuzz()
self.number_turn.release()
# printFizzBuzz() outputs "fizzbuzz"
def fizzbuzz(self, printFizzBuzz: 'Callable[[], None]') -> None:
for _ in range(15, self.n + 1, 15):
self.fizzbuzz_turn.acquire()
printFizzBuzz()
self.number_turn.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.number_turn.acquire()
if i % 15 == 0:
self.fizzbuzz_turn.release()
elif i % 3 == 0:
self.fizz_turn.release()
elif i % 5 == 0:
self.buzz_turn.release()
else:
printNumber(i)
self.number_turn.release()