-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1195.java
68 lines (60 loc) · 2.04 KB
/
1195.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
class FizzBuzz {
private int n;
private int currentNumber;
public FizzBuzz(int n) {
this.n = n;
this.currentNumber = 1;
}
// fizz" if i is divisible by 3 and not 5,
// printFizz.run() outputs "fizz".
public synchronized void fizz(Runnable printFizz) throws InterruptedException {
while (this.currentNumber <= n) {
if (this.currentNumber % 3 == 0 && this.currentNumber % 5 != 0) {
printFizz.run();
this.currentNumber++;
notifyAll();
} else {
wait();
}
}
}
// "buzz" if i is divisible by 5 and not 3
// printBuzz.run() outputs "buzz".
public synchronized void buzz(Runnable printBuzz) throws InterruptedException {
while (this.currentNumber <= n) {
if (this.currentNumber % 3 != 0 && this.currentNumber % 5 == 0) {
printBuzz.run();
this.currentNumber++;
notifyAll();
} else {
wait();
}
}
}
// "fizzbuzz" if i is divisible by 3 and 5
// printFizzBuzz.run() outputs "fizzbuzz".
public synchronized void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
while (this.currentNumber <= n) {
if (this.currentNumber % 3 == 0 && this.currentNumber % 5 == 0) {
this.currentNumber++;
printFizzBuzz.run();
notifyAll();
} else {
wait();
}
}
}
// i if i is not divisible by 3 or 5
// printNumber.accept(x) outputs "x", where x is an integer.
public synchronized void number(IntConsumer printNumber) throws InterruptedException {
while (this.currentNumber <= n) {
if (this.currentNumber % 3 != 0 && this.currentNumber % 5 != 0) {
printNumber.accept(this.currentNumber);
this.currentNumber++;
notifyAll();
} else {
wait();
}
}
}
}