Skip to content

Commit 4eb8b4f

Browse files
committed
volatile的可见性
1 parent 203078d commit 4eb8b4f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

thread/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@
114114

115115
-------------------------
116116

117+
## [volatile](src/main/java/com/cpucode/java/volati)
118+
119+
- [x] [volatile的可见性](src/main/java/com/cpucode/java/volati/VolatileDemo.java)
120+
121+
- [返回文件目录](#文件目录)
122+
123+
-------------------------
117124

118125
- [x] [Thread1__创建新线程](src/main/java/com/cpucode/java/thread/Thread1.java)
119126
- [x] [Thread2__自定义线程](src/main/java/com/cpucode/java/thread/Thread2.java)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.cpucode.java.volati;
2+
3+
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
4+
5+
/**
6+
* @author : cpucode
7+
* @date : 2021/7/28
8+
* @time : 10:26
9+
* @github : https://github.com/CPU-Code
10+
* @csdn : https://blog.csdn.net/qq_44226094
11+
*/
12+
public class VolatileDemo {
13+
//public volatile static boolean stop = false;
14+
public static boolean stop = false;
15+
16+
public static void main(String[] args) throws InterruptedException {
17+
Thread thread = new Thread(() -> {
18+
int i = 0;
19+
20+
while(!stop){
21+
i++;
22+
}
23+
});
24+
25+
thread.start();
26+
27+
System.out.println("begin start thread");
28+
29+
Thread.sleep(1000);
30+
stop = true;
31+
}
32+
}

0 commit comments

Comments
 (0)