-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFizzBuzzMultiThreaded.java
72 lines (66 loc) · 2.52 KB
/
FizzBuzzMultiThreaded.java
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
/*https://leetcode.com/problems/fizz-buzz-multithreaded/*/
class FizzBuzz {
private int n;
Semaphore fizzBlock, buzzBlock, fizzBuzzBlock, numberRun, numberBlock;
public FizzBuzz(int n) {
this.n = n;
fizzBlock = new Semaphore(0); //blocking semaphore
buzzBlock = new Semaphore(0); //blocking semaphore
fizzBuzzBlock = new Semaphore(0); //blocking semaphore
numberRun = new Semaphore(1); //binary semaphore
}
// printFizz.run() outputs "fizz".
public void fizz(Runnable printFizz) throws InterruptedException {
int loop = (n/3)-(n/15);
for (int i = 0; i < loop; ++i)
{
fizzBlock.acquire(); //block
printFizz.run(); //this line runs only when the above blocking semaphore is released
numberRun.release(); //release the binary semaphore for next iteration
}
}
// printBuzz.run() outputs "buzz".
public void buzz(Runnable printBuzz) throws InterruptedException {
int loop = (n/5)-(n/15);
for (int i = 0; i < loop; ++i)
{
buzzBlock.acquire(); //block
printBuzz.run(); //this line runs only when the above blocking semaphore is released
numberRun.release(); //release the binary semaphore for next iteration
}
}
// printFizzBuzz.run() outputs "fizzbuzz".
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
int loop = n/15;
for (int i = 0; i < loop; ++i)
{
fizzBuzzBlock.acquire(); //block
printFizzBuzz.run(); //this line runs only when the above blocking semaphore is released
numberRun.release(); //release the binary semaphore for next iteration
}
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void number(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; ++i)
{
numberRun.acquire(); //acquire binary semaphore
if (i%15 == 0) //for 15
{
fizzBuzzBlock.release(); //release fizz buzz
}
else if (i%3 == 0) //for 3
{
fizzBlock.release(); //release fizz
}
else if (i%5 == 0) //for 5
{
buzzBlock.release(); //release buzz
}
else //otherwise
{
printNumber.accept(i); //print the number
numberRun.release(); //release the binary semaphore
}
}
}
}