-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolatileDemo.java
More file actions
33 lines (28 loc) · 781 Bytes
/
VolatileDemo.java
File metadata and controls
33 lines (28 loc) · 781 Bytes
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
package concurrent.base;
/**
* @author zhongshanhuang
* @company caih
* @email [email protected]
* @create 2019-11-06 0:21
*/
public class VolatileDemo {
private static volatile boolean flag = false;
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
while (!flag) {
try {
Thread.sleep(300L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("flag is false");
}
System.out.println("flag is true");
}).start();
Thread.sleep(3000L);
//开关
new Thread(() -> {
flag = true;
}).start();
}
}