-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFizzBuzzMutliThread.java
127 lines (111 loc) · 3.88 KB
/
FizzBuzzMutliThread.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package TDD;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntConsumer;
public class FizzBuzzMutliThread {
public static void main(String[] args) {
FizzBuzz fizzBuzz = new FizzBuzz(15);
Runnable printFizz = () -> System.out.println("fizz");
Runnable printBuzz = () -> System.out.println("buzz");
Runnable printFizzBuzz = () -> System.out.println("fizzbuzz");
IntConsumer printNumber = number -> System.out.println(number);
Thread threadA = new Thread(() -> {
try {
fizzBuzz.fizz(printFizz);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread threadB = new Thread(() -> {
try {
fizzBuzz.buzz(printBuzz);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread threadC = new Thread(() -> {
try {
fizzBuzz.fizzbuzz(printFizzBuzz);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread threadD = new Thread(() -> {
try {
fizzBuzz.number(printNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
threadA.start();
threadB.start();
threadC.start();
threadD.start();
}
public static class FizzBuzz {
private int n;
private Semaphore lock;
private AtomicInteger counter;
public FizzBuzz(int n) {
this.n = n;
this.lock = new Semaphore(1);
this.counter = new AtomicInteger(1);
}
// printFizz.run() outputs "fizz".
public void fizz(Runnable printFizz) throws InterruptedException {
int step = n/3 - n/15;
int i = 0;
while (i < step) {
lock.acquire();
if (counter.get() % 3 == 0 && counter.get() % 15 != 0) {
printFizz.run();
counter.incrementAndGet();
i++;
}
lock.release();
}
}
// printBuzz.run() outputs "buzz".
public void buzz(Runnable printBuzz) throws InterruptedException {
int step = n/5 - n/15;
int i = 0;
while (i < step) {
lock.acquire();
if (counter.get() % 5 == 0 && counter.get() % 15 != 0) {
printBuzz.run();
counter.incrementAndGet();
i++;
}
lock.release();
}
}
// printFizzBuzz.run() outputs "fizzbuzz".
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
int step = n/15;
int i = 0;
while (i < step) {
lock.acquire();
if (counter.get() % 15 == 0) {
printFizzBuzz.run();
counter.incrementAndGet();
i++;
}
lock.release();
}
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void number(IntConsumer printNumber) throws InterruptedException {
int step = n - n/3 - n/5 + n/15;
int i = 0;
while (i < step) {
lock.acquire();
if (counter.get() % 3 != 0 && counter.get() % 5 != 0) {
printNumber.accept(counter.get());
counter.incrementAndGet();
i++;
}
lock.release();
}
}
}
}