-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrintFooBarAlternately.java
More file actions
212 lines (186 loc) · 6.23 KB
/
PrintFooBarAlternately.java
File metadata and controls
212 lines (186 loc) · 6.23 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// 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.
// See: https://leetcode.com/problems/print-foobar-alternately/
package leetcode.concurrency;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class PrintFooBarAlternately {
/**
* Solution 4: Semaphores - simple and clean, without verbose wait/notify/signal mechanism and while loops.
*/
class FooBar {
private int n;
private Semaphore semFoo = new Semaphore(1);
private Semaphore semBar = new Semaphore(0);
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
semFoo.acquire();
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
semBar.release();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
// because the initial value of the "semBar" is 0 the tread will wait here until "semBar.release();"
semBar.acquire();
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
semFoo.release();
}
}
}
/**
* Solution 3: The good old wait/notify
*/
class FooBar3 {
private int n;
private volatile boolean foo = true;
private Object sync = new Object();
public FooBar3(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (sync) {
while (!foo)
sync.wait();
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
foo = false;
sync.notifyAll();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (sync) {
while (foo)
sync.wait();
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
foo = true;
sync.notifyAll();
}
}
}
}
/**
* Solution 2: Lock/await/signalAll
*/
class FooBar2 {
private int n;
private ReentrantLock lock = new ReentrantLock();
private Condition fooCond = lock.newCondition();
private volatile boolean foo = true;
public FooBar2(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
try {
lock.lock();
while (!foo)
fooCond.await();
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
foo = false;
fooCond.signalAll();
} finally {
lock.unlock();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
try {
lock.lock();
while (foo)
fooCond.await();
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
foo = true;
fooCond.signalAll();
} finally {
lock.unlock();
}
}
}
/**
* Solution 1: Simple boolean condition
*/
class FooBar1 {
private int n;
private volatile boolean foo = true;
public FooBar1(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
// printFoo.run() outputs "foo". Do not change or remove this line.
while (!foo) {
}
printFoo.run();
foo = false;
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (foo) {
}
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
foo = true;
}
}
}
}
public static void main(String[] args) {
FooBar foobar = new PrintFooBarAlternately().new FooBar(2);
Thread th1 = new Thread(() -> {
try {
foobar.foo(new Runnable() {
@Override
public void run() {
System.out.print("foo");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread th2 = new Thread(() -> {
try {
foobar.bar(new Runnable() {
@Override
public void run() {
System.out.print("bar");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
});
th1.start();
th2.start();
}
}