forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT6.java
More file actions
42 lines (33 loc) · 940 Bytes
/
T6.java
File metadata and controls
42 lines (33 loc) · 940 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
34
35
36
37
38
39
40
41
42
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: volatile关键字
* @author: mf
* @create: 2019/12/28 22:54
*/
public class T6 {
/*volatile*/ boolean running = true; // 对比一下有无valotile的情况下,整个程序运行结果的区别
void m() {
System.out.println("m start");
while (running) {
// 啥都没得
// try {
// TimeUnit.SECONDS.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
System.out.println("m end ...");
}
public static void main(String[] args) {
T6 t6 = new T6();
new Thread(() -> t6.m(), "t1").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
t6.running = false;
}
}