-
Notifications
You must be signed in to change notification settings - Fork 14
/
FooBarNTimes.java
66 lines (59 loc) · 1.69 KB
/
FooBarNTimes.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
package by.andd3dfx.multithreading;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import java.io.StringWriter;
import java.util.concurrent.Semaphore;
/**
* <pre>
* <a href="https://leetcode.com/problems/print-foobar-alternately/">Task description</a>
*
* Suppose you are given the following code:
* class FooBar {
* public void foo() {
* for (int i = 0; i < n; i++) {
* print("foo");
* }
* }
*
* public void bar() {
* for (int i = 0; i < n; i++) {
* print("bar");
* }
* }
* }
*
* The same instance of FooBar will be passed to two different threads:
* thread A will call foo(), while
* thread B will call bar().
* Modify the given program to output "foobar" n times.
* </pre>
*
* @see <a href="https://youtu.be/UVrrfYTiRo8">Video solution</a>
*/
public class FooBarNTimes {
@RequiredArgsConstructor
public static class FooBar {
private final int n;
@Getter
private final StringWriter logWriter = new StringWriter();
private Semaphore fooSemaphore = new Semaphore(1);
private Semaphore barSemaphore = new Semaphore(0);
@SneakyThrows
public void foo() {
for (int i = 0; i < n; i++) {
fooSemaphore.acquire();
logWriter.write("foo");
barSemaphore.release();
}
}
@SneakyThrows
public void bar() {
for (int i = 0; i < n; i++) {
barSemaphore.acquire();
logWriter.write("bar");
fooSemaphore.release();
}
}
}
}