-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlternatePrintAB.java
More file actions
55 lines (50 loc) · 1.52 KB
/
AlternatePrintAB.java
File metadata and controls
55 lines (50 loc) · 1.52 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
package multithreading;
/**
* @Author: Wenhang Chen
* @Description:交替打印AB,synchronized实现
* @Date: Created in 15:11 4/4/2020
* @Modified by:
*/
public class AlternatePrintAB {
// 写一个锁
private final static Object lock = new Object();
// 一个状态标记
private static boolean state = true;
static class Task implements Runnable {
private String name;
private boolean flag;
// 初始化
public Task(String name, boolean flag) {
this.name = name;
this.flag = flag;
}
@Override
public void run() {
int i = 0;
while (i < 5) {
// 锁住代码块
synchronized (lock) {
// 先写不满足的情况,等待
while (flag != state) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 再写满足的情况,注意更新状态,并唤醒其他线程
state = !state;
System.out.println(name);
lock.notifyAll();
}
i++;
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Task("A", true));
Thread t2 = new Thread(new Task("B", false));
t1.start();
t2.start();
}
}