Skip to content

Commit a4881bf

Browse files
committed
Improved task 1195
1 parent 7458eef commit a4881bf

File tree

2 files changed

+75
-43
lines changed

2 files changed

+75
-43
lines changed

src/main/java/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzz.java

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,75 @@
11
package g1101_1200.s1195_fizz_buzz_multithreaded;
22

3-
// #Medium #Concurrency #2022_03_03_Time_8_ms_(80.09%)_Space_43.2_MB_(6.17%)
3+
// #Medium #Concurrency #2024_11_24_Time_6_ms_(94.88%)_Space_43.1_MB_(8.61%)
44

5-
import java.util.concurrent.atomic.AtomicInteger;
65
import java.util.function.IntConsumer;
76

87
@SuppressWarnings("java:S1130")
98
public class FizzBuzz {
10-
private final AtomicInteger count = new AtomicInteger(1);
11-
129
private final int n;
10+
private int current;
1311

1412
public FizzBuzz(int n) {
1513
this.n = n;
14+
this.current = 1;
1615
}
1716

1817
// printFizz.run() outputs "fizz".
19-
public void fizz(Runnable printFizz) {
20-
int i;
21-
while ((i = count.get()) <= n) {
22-
if (i % 3 == 0 && i % 5 != 0) {
23-
printFizz.run();
24-
count.compareAndSet(i, i + 1);
18+
public void fizz(Runnable printFizz) throws InterruptedException {
19+
synchronized (this) {
20+
while (current <= n) {
21+
if (current % 3 == 0 && current % 5 != 0) {
22+
printFizz.run();
23+
current += 1;
24+
notifyAll();
25+
} else {
26+
wait();
27+
}
2528
}
2629
}
2730
}
2831

2932
// printBuzz.run() outputs "buzz".
30-
public void buzz(Runnable printBuzz) {
31-
int i;
32-
while ((i = count.get()) <= n) {
33-
count.get();
34-
if (i % 5 == 0 && i % 3 != 0) {
35-
printBuzz.run();
36-
count.compareAndSet(i, i + 1);
33+
public void buzz(Runnable printBuzz) throws InterruptedException {
34+
synchronized (this) {
35+
while (current <= n) {
36+
if (current % 3 != 0 && current % 5 == 0) {
37+
printBuzz.run();
38+
current += 1;
39+
notifyAll();
40+
} else {
41+
wait();
42+
}
3743
}
3844
}
3945
}
4046

4147
// printFizzBuzz.run() outputs "fizzbuzz".
42-
public void fizzbuzz(Runnable printFizzBuzz) {
43-
int i;
44-
while ((i = count.get()) <= n) {
45-
if (i % 15 == 0) {
46-
printFizzBuzz.run();
47-
count.compareAndSet(i, i + 1);
48+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
49+
synchronized (this) {
50+
while (current <= n) {
51+
if (current % 15 == 0) {
52+
printFizzBuzz.run();
53+
current += 1;
54+
notifyAll();
55+
} else {
56+
wait();
57+
}
4858
}
4959
}
5060
}
5161

5262
// printNumber.accept(x) outputs "x", where x is an integer.
53-
public void number(IntConsumer printNumber) {
54-
int i;
55-
while ((i = count.get()) <= n) {
56-
if (i % 5 != 0 && i % 3 != 0) {
57-
printNumber.accept(i);
58-
count.compareAndSet(i, i + 1);
63+
public void number(IntConsumer printNumber) throws InterruptedException {
64+
synchronized (this) {
65+
while (current <= n) {
66+
if (current % 3 != 0 && current % 5 != 0) {
67+
printNumber.accept(current);
68+
current += 1;
69+
notifyAll();
70+
} else {
71+
wait();
72+
}
5973
}
6074
}
6175
}

src/test/java/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@ void fizzBuzz() throws InterruptedException {
1414
FizzBuzz fizzBuzz = new FizzBuzz(15);
1515
new Thread(
1616
() -> {
17-
fizzBuzz.fizz(() -> fizz[0]++);
17+
try {
18+
fizzBuzz.fizz(() -> fizz[0]++);
19+
} catch (InterruptedException e) {
20+
}
1821
})
1922
.start();
2023
new Thread(
2124
() -> {
22-
fizzBuzz.buzz(() -> fizz[0]++);
25+
try {
26+
fizzBuzz.buzz(() -> fizz[0]++);
27+
} catch (InterruptedException e) {
28+
}
2329
})
2430
.start();
2531
new Thread(
2632
() -> {
27-
fizzBuzz.fizzbuzz(() -> fizz[0]++);
33+
try {
34+
fizzBuzz.fizzbuzz(() -> fizz[0]++);
35+
} catch (InterruptedException e) {
36+
}
2837
})
2938
.start();
3039
new Thread(
3140
() -> {
32-
fizzBuzz.number(
33-
value -> {
34-
fizz[0]++;
35-
});
41+
try {
42+
fizzBuzz.number(value -> fizz[0]++);
43+
} catch (InterruptedException e) {
44+
}
3645
})
3746
.start();
3847
TimeUnit.MILLISECONDS.sleep(2000);
@@ -45,25 +54,34 @@ void fizzBuzz2() throws InterruptedException {
4554
FizzBuzz fizzBuzz = new FizzBuzz(5);
4655
new Thread(
4756
() -> {
48-
fizzBuzz.fizz(() -> fizz[0]++);
57+
try {
58+
fizzBuzz.fizz(() -> fizz[0]++);
59+
} catch (InterruptedException e) {
60+
}
4961
})
5062
.start();
5163
new Thread(
5264
() -> {
53-
fizzBuzz.buzz(() -> fizz[0]++);
65+
try {
66+
fizzBuzz.buzz(() -> fizz[0]++);
67+
} catch (InterruptedException e) {
68+
}
5469
})
5570
.start();
5671
new Thread(
5772
() -> {
58-
fizzBuzz.fizzbuzz(() -> fizz[0]++);
73+
try {
74+
fizzBuzz.fizzbuzz(() -> fizz[0]++);
75+
} catch (InterruptedException e) {
76+
}
5977
})
6078
.start();
6179
new Thread(
6280
() -> {
63-
fizzBuzz.number(
64-
value -> {
65-
fizz[0]++;
66-
});
81+
try {
82+
fizzBuzz.number(value -> fizz[0]++);
83+
} catch (InterruptedException e) {
84+
}
6785
})
6886
.start();
6987
TimeUnit.MILLISECONDS.sleep(600);

0 commit comments

Comments
 (0)