-
Notifications
You must be signed in to change notification settings - Fork 2
/
1195. Fizz Buzz Multithreaded.py
86 lines (74 loc) · 2.86 KB
/
1195. Fizz Buzz Multithreaded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Write a program that outputs the string representation of numbers from 1 to n, however:
If the number is divisible by 3, output "fizz".
If the number is divisible by 5, output "buzz".
If the number is divisible by both 3 and 5, output "fizzbuzz".
For example, for n = 15, we output: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz.
Suppose you are given the following code:
class FizzBuzz {
public FizzBuzz(int n) { ... } // constructor
public void fizz(printFizz) { ... } // only output "fizz"
public void buzz(printBuzz) { ... } // only output "buzz"
public void fizzbuzz(printFizzBuzz) { ... } // only output "fizzbuzz"
public void number(printNumber) { ... } // only output the numbers
}
Implement a multithreaded version of FizzBuzz with four threads. The same instance of FizzBuzz will be passed to four different threads:
Thread A will call fizz() to check for divisibility of 3 and outputs fizz.
Thread B will call buzz() to check for divisibility of 5 and outputs buzz.
Thread C will call fizzbuzz() to check for divisibility of 3 and 5 and outputs fizzbuzz.
Thread D will call number() which should only output the numbers.
"""
class FizzBuzz(object):
def __init__(self, n):
self.n = n
self.number = threading.Semaphore(0)
self.fizz = threading.Semaphore(0)
self.buzz = threading.Semaphore(0)
self.fizzbuzz = threading.Semaphore(1)
# printFizz() outputs "fizz"
def fizz(self, printFizz):
"""
:type printFizz: method
:rtype: void
"""
for j in range(self.n//3-self.n//15):
self.fizz.acquire()
printFizz()
self.number.release()
# printBuzz() outputs "buzz"
def buzz(self, printBuzz):
"""
:type printBuzz: method
:rtype: void
"""
for j in range(self.n//5-self.n//15):
self.buzz.acquire()
printBuzz()
self.number.release()
# printFizzBuzz() outputs "fizzbuzz"
def fizzbuzz(self, printFizzBuzz):
"""
:type printFizzBuzz: method
:rtype: void
"""
for j in range(self.n//3-self.n//15):
self.fizzbuzz.acquire()
printFizzBuzz()
self.number.release()
# printNumber(x) outputs "x", where x is an integer.
def number(self, printNumber):
"""
:type printNumber: method
:rtype: void
"""
for i in range(1,self.n+1):
self.number.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.number.release()