forked from yihaoye/data-structure-and-algorithm-study-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1115 Print FooBar Alternately.java
More file actions
193 lines (153 loc) · 4.96 KB
/
1115 Print FooBar Alternately.java
File metadata and controls
193 lines (153 loc) · 4.96 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
/**
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.
Example 1:
Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
"foobar" is being output 1 time.
Example 2:
Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times.
Constraints:
1 <= n <= 1000
*/
// Other's Solution:
// https://leetcode.com/problems/print-foobar-alternately/discuss/348546/Java-4-Java-threading-solutions-(SynchronizedLockVolatileCAS)
// Synchronized(monitor exit happens-before monitor enter)
public class FooBarSynchronized {
private int n;
// flag 0->foo to be print 1->foo has been printed
private int flag = 0;
public FooBarSynchronized(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (this) {
while (flag == 1) {
this.wait();
}
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
flag = 1;
this.notifyAll();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (this) {
while (flag == 0) {
this.wait();
}
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
flag = 0;
this.notifyAll();
}
}
}
}
// Lock(volatile write happens-before volatile read)
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class FooBar {
private int n;
// flag 0->foo to be print 1->foo has been printed
private int flag = 0;
ReentrantLock reentrantLock = new ReentrantLock();
Condition fooPrintedCondition = reentrantLock.newCondition();
Condition barPrintedCondition = reentrantLock.newCondition();
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
reentrantLock.lock();
while (flag == 1) barPrintedCondition.await();
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
flag = 1;
fooPrintedCondition.signalAll();
reentrantLock.unlock();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
reentrantLock.lock();
while (flag == 0) fooPrintedCondition.await();
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
flag = 0;
barPrintedCondition.signalAll();
reentrantLock.unlock();
}
}
}
// CAS (volatile write happens-before volatile read)
import java.util.concurrent.atomic.AtomicInteger;
public class FooBarCAS {
private int n;
// flag 0->foo to be print 1->foo has been printed
private AtomicInteger flag = new AtomicInteger(0);
public FooBarCAS(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (!flag.compareAndSet(0, 1)) {
Thread.sleep(1);
}
printFoo.run();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (!flag.compareAndSet(1, 0)) {
Thread.sleep(1);
}
printBar.run();
}
}
}
// My Solution after inspired:
class FooBar {
private int n;
private volatile boolean lock; // when lock = false, can only print foo, otherwise can only print bar. Voatile (volatile write happens-before volatile read)
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (lock);
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
lock = true;
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (!lock);
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
lock = false;
}
}
}