-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1114_PrintInOrder.java
More file actions
119 lines (110 loc) · 3.59 KB
/
P1114_PrintInOrder.java
File metadata and controls
119 lines (110 loc) · 3.59 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
package yyl.leetcode.p11;
/**
* <h3>按序打印</h3><br>
* 我们提供了一个类:
*
* <pre>
* public class Foo {
* public void one() {
* print("one");
* }
*
* public void two() {
* print("two");
* }
*
* public void three() {
* print("three");
* }
* }
* </pre>
*
* 三个不同的线程将会共用一个 Foo 实例。 <br>
* 线程 A 将会调用 one() 方法 <br>
* 线程 B 将会调用 two() 方法 <br>
* 线程 C 将会调用 three() 方法<br>
* 请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。
*
* <pre>
* 示例 1:
* 输入: [1,2,3]
* 输出: "onetwothree"
* 解释:
* 有三个线程会被异步启动。
* 输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
* 正确的输出是 "onetwothree"。
*
* 示例 2:
* 输入: [1,3,2]
* 输出: "onetwothree"
* 解释:
* 输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
* 正确的输出是 "onetwothree"。
* </pre>
*/
public class P1114_PrintInOrder {
public static void main(String[] args) {
final Foo foo = new Foo();
Runnable[] runnables = { //
() -> foo.first(() -> System.out.print("one")), //
() -> foo.second(() -> System.out.print("two")), //
() -> foo.third(() -> System.out.print("three")), //
};
int[] ids = { 0, 2, 1 };
for (int id : ids) {
new Thread(runnables[id]).start();
}
}
// 思路:利用 wait() 和 notifyAll() 保证顺序
static class Foo {
private final Object lock = new Object();
private int signal = 1;
public Foo() {
}
public void first(Runnable printFirst) {
synchronized (lock) {
while (signal != 1) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
signal = 2;
lock.notifyAll();
}
}
public void second(Runnable printSecond) {
synchronized (lock) {
while (signal != 2) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
signal = 3;
lock.notifyAll();
}
}
public void third(Runnable printThird) {
synchronized (lock) {
while (signal != 3) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
signal = 1;
lock.notifyAll();
}
}
}
}