1115. 交替打印FooBar

题目

我们提供了一个类:

1
2
3
4
5
6
7
8
9
10
11
12
13
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");
}
}
}

两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。

请设计修改程序,以确保 “foobar” 被输出 n 次。

示例1:

1
2
3
输入: n = 1
输出: "foobar"
解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。

示例2:

1
2
3
输入: n = 2
输出: "foobarfoobar"
解释: "foobar" 将被输出两次。

解法

解法一:

(超时)

while循环死等

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
class FooBar {

private transient int flag = 1;

private int n;

public FooBar(int n) {
this.n = n;
}

public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
while (true) {
if (1 == flag) {
break;
}
}
// printFoo.run() outputs "foo". Do not change or remove this
// line.
printFoo.run();
flag = 2;
}
}

public void bar(Runnable printBar) throws InterruptedException {

for (int i = 0; i < n; i++) {
while (true) {
if (2 == flag) {
break;
}
}
// printBar.run() outputs "bar". Do not change or remove this
// line.
printBar.run();
flag = 1;
}
}
}

解法二:

使用Condition做精准唤醒

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
class FooBar {

private int flag = 1;

ReentrantLock lock = new ReentrantLock();
Condition c1 = lock.newCondition();
Condition c2 = lock.newCondition();

private int n;

public FooBar(int n) {
this.n = n;
}

public void foo(Runnable printFoo) throws InterruptedException {
try {
lock.lock();

for (int i = 0; i < n; i++) {
while (1 != flag) {
c1.await();
}
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
flag = 2;
c2.signal();
}
} finally {
lock.unlock();
}
}

public void bar(Runnable printBar) throws InterruptedException {

try {
lock.lock();
for (int i = 0; i < n; i++) {
while (2 != flag) {
c2.await();
}
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
flag = 1;
c1.signal();
}
} finally {
lock.unlock();
}
}
}

解法三:

使用wait和notify

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
class FooBar {

private Object lock = new Object();

private int flag = 1;

private int n;

public FooBar(int n) {
this.n = n;
}

public void foo(Runnable printFoo) throws InterruptedException {
synchronized (lock) {
for (int i = 0; i < n; i++) {
if (1 != flag) {
lock.wait();
}
// printFoo.run() outputs "foo". Do not change or remove
// this
// line.
printFoo.run();
flag = 2;
lock.notifyAll();
}
}
}

public void bar(Runnable printBar) throws InterruptedException {

synchronized (lock) {
for (int i = 0; i < n; i++) {
if (2 != flag) {
lock.wait();
}
// printBar.run() outputs "bar". Do not change or remove
// this
// line.
printBar.run();
flag = 1;
lock.notifyAll();
}
}

}
}

解法四:

使用CyclicBarrier

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
class FooBar {
private int n;

public FooBar(int n) {
this.n = n;
}

CyclicBarrier cb = new CyclicBarrier(2);
volatile boolean fin = true;

public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
while(!fin);
printFoo.run();
fin = false;
try {
cb.await();
} catch (BrokenBarrierException e) {
}
}
}

public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
try {
cb.await();
} catch (BrokenBarrierException e) {
}
printBar.run();
fin = true;
}
}
}

解法五:

使用Semaphore

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
class FooBar {

private Semaphore s1 = new Semaphore(0);
private Semaphore s2 = new Semaphore(1);

private int n;

public FooBar(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.
s2.acquire();
printFoo.run();
s1.release();
}
}

public void bar(Runnable printBar) throws InterruptedException {

for (int i = 0; i < n; i++) {
// printBar.run() outputs "bar". Do not change or remove
// this
// line.
s1.acquire();
printBar.run();
s2.release();
}
}
}
0%