forked from zaiyunduan123/Java-Summarize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitNotifyTest.java
More file actions
65 lines (60 loc) · 2.52 KB
/
WaitNotifyTest.java
File metadata and controls
65 lines (60 loc) · 2.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
56
57
58
59
60
61
62
63
64
65
package concurrent.notify;
/**
* @Auther: Jesper
* @Date: 2019/1/31 10:34
* @Description: 启动两个线程, 一个输出 1,3,5,7…99, 另一个输出 2,4,6,8…100 最后 STDOUT 中按序输出 1,2,3,4,5…100
*
* 用 Java 的 notify 机制实现
*/
public class WaitNotifyTest {
private static int state = 1; // 通状态值来实现两个线程交替打印
private static int num1 = 1; // 线程1 要打印的数值
private static int num2 = 2; // 线程2 要打印的数值
public static void main(String[] args) {
final WaitNotifyTest t = new WaitNotifyTest();
new Thread(new Runnable() {
@Override
public void run() {
while (num1 < 100) {
//两个线程都用t对象作为锁,保证每个交替期间只有一个线程在打印
synchronized (t) {
if (state != 1) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ":" + num1);
num1 += 2; // 因为是交替打印,所以每次打印完,值需要+2
// 线程1打印完成后, 将state赋值为2, 表示接下来将轮到线程2打印
state = 2;
// notify()方法唤醒在t上wait的线程2, 同时线程1将退出同步代码块, 释放t锁
t.notify();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (num2 < 100) {
//两个线程都用t对象作为锁,保证每个交替期间只有一个线程在打印
synchronized (t) {
if (state != 2) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ":" + num2);
num2 += 2;
state = 1;
t.notify();
}
}
}
}).start();
}
}