-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFizzBuzzMultithreaded.py
75 lines (66 loc) · 2.28 KB
/
FizzBuzzMultithreaded.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
import threading
from typing import Callable
class FizzBuzz:
def __init__(self, n: int):
self.n = n
self.done = False
self.fizzLock = threading.Lock()
self.buzzLock = threading.Lock()
self.fizzbuzzLock = threading.Lock()
self.numberLock = threading.Lock()
self.fizzLock.acquire()
self.buzzLock.acquire()
self.fizzbuzzLock.acquire()
# printFizz() outputs "fizz"
def fizz(self, printFizz: 'Callable[[], None]') -> None:
while True:
self.fizzLock.acquire()
if self.done:
return
printFizz()
self.numberLock.release()
# printBuzz() outputs "buzz"
def buzz(self, printBuzz: 'Callable[[], None]') -> None:
while True:
self.buzzLock.acquire()
if self.done:
return
printBuzz()
self.numberLock.release()
# printFizzBuzz() outputs "fizzbuzz"
def fizzbuzz(self, printFizzBuzz: 'Callable[[], None]') -> None:
while True:
self.fizzbuzzLock.acquire()
if self.done:
return
printFizzBuzz()
self.numberLock.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.numberLock.acquire()
if i % 15 == 0:
self.fizzbuzzLock.release()
elif i % 3 == 0:
self.fizzLock.release()
elif i % 5 == 0:
self.buzzLock.release()
else:
printNumber(i)
self.numberLock.release()
self.numberLock.acquire()
self.done = True
self.fizzLock.release()
self.buzzLock.release()
self.fizzbuzzLock.release()
return
if __name__ == '__main__':
fizzBuzz = FizzBuzz(15)
t1 = threading.Thread(target=fizzBuzz.fizz, args=(lambda: print("fizz"),))
t2 = threading.Thread(target=fizzBuzz.buzz, args=(lambda: print("buzz"),))
t3 = threading.Thread(target=fizzBuzz.fizzbuzz, args=(lambda: print("fizzbuzz"),))
t4 = threading.Thread(target=fizzBuzz.number, args=(lambda x: print(x),))
t1.start()
t2.start()
t3.start()
t4.start()