-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1115_PrintFoobarAlternately.java
More file actions
96 lines (89 loc) · 2.8 KB
/
P1115_PrintFoobarAlternately.java
File metadata and controls
96 lines (89 loc) · 2.8 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
package yyl.leetcode.p11;
/**
* <h3>交替打印FooBar</h3><bre> 我们提供一个类:
*
* <pre>
* 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");
* }
* }
* }
* </pre>
*
* 两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。<bre> 请设计修改程序,以确保 "foobar" 被输出 n 次。<bre>
*
* <pre>
* 示例 1:
* 输入: n = 1
* 输出: "foobar"
* 解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
*
* 示例 2:
* 输入: n = 2
* 输出: "foobarfoobar"
* 解释: "foobar" 将被输出两次。
* </pre>
*/
public class P1115_PrintFoobarAlternately {
public static void main(String[] args) {
Runnable printFoo = () -> System.out.print("foo");
Runnable printBar = () -> System.out.print("bar");
FooBar sample = new FooBar(2);
new Thread(() -> {
try {
sample.foo(printFoo);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
sample.bar(printBar);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
static class FooBar {
private final int n;
private Object lock = new Object();
private int signal = 0;
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (lock) {
while (signal != 0) {
lock.wait();
}
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
signal = 1;
lock.notifyAll();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (lock) {
while (signal != 1) {
lock.wait();
}
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
signal = 0;
lock.notifyAll();
}
}
}
}
}