GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refine volatile demo · kings186/PassJava-Learning@e50ce0a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • code/01.Threads/src/com/jackson0714/passjava/threads
    • VolatileResort.java
    • VolatileVisibility.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎code/01.Threads/src/com/jackson0714/passjava/threads/VolatileResort.java‎

Copy file name to clipboard
+36-31Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
package com.jackson0714.passjava.threads;
2-
3-
/**
4-
演示volatile 禁止重排
5-
* @author: 悟空聊架构
6-
* @create: 2020-08-13
7-
*/
8-
9-
10-
public class VolatileResort {
11-
static int num = 0;
12-
static volatile boolean flag = false;
13-
public static void init() {
14-
num= 1;
15-
flag = true;
16-
}
17-
public static void add() {
18-
while (flag) {
19-
flag = false;
20-
num = num + 100;
21-
System.out.println("num:" + num);
22-
}
23-
}
24-
public static void main(String[] args) throws InterruptedException {
25-
new Thread(() -> {
26-
add();
27-
},"子线程").start();
28-
init();
29-
30-
}
31-
}
1+
package com.jackson0714.passjava.threads;
2+
3+
/**
4+
演示volatile 禁止重排
5+
* @author: 悟空聊架构
6+
* @create: 2020-08-13
7+
*/
8+
9+
10+
public class VolatileResort {
11+
static int num = 0;
12+
static boolean flag = false;
13+
public static void init() {
14+
num = 1;
15+
flag = true;
16+
}
17+
public static void add() {
18+
while (flag) {
19+
num = num + 5;
20+
flag = false;
21+
System.out.println("num:" + num);
22+
}
23+
}
24+
public static void main(String[] args) throws InterruptedException {
25+
Thread t1 = new Thread(() -> {
26+
add();
27+
},"子线程 t1");
28+
29+
Thread t2 = new Thread(() -> {
30+
init();
31+
},"子线程 t2");
32+
33+
t2.start();
34+
t1.start();
35+
}
36+
}
Collapse file

‎code/01.Threads/src/com/jackson0714/passjava/threads/VolatileVisibility.java‎

Copy file name to clipboard
+71-69Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,71 @@
1-
package com.jackson0714.passjava.threads;
2-
import java.util.concurrent.TimeUnit;
3-
/**
4-
* Volatile Java虚拟机提供的轻量级同步机制
5-
*
6-
* 可见性(及时通知)
7-
* 不保证原子性
8-
* 禁止指令重排
9-
* @author: 悟空聊架构
10-
* @create: 2020-08-12
11-
*/
12-
13-
14-
class ShareData {
15-
//volatile 修饰的关键字,是为了增加多个线程之间的可见性,只要有一个线程修改了内存中的值,其它线程也能马上感知
16-
volatile int number = 0;
17-
18-
public void setNumberTo100() {
19-
this.number = 100;
20-
}
21-
}
22-
23-
/**
24-
* 验证volatile的可见性
25-
* 1. 假设int number = 0, number变量之前没有添加volatile关键字修饰,没有可见性
26-
* 2. 添加了volatile,可以解决可见性问题
27-
*/
28-
public class VolatileVisibility {
29-
public static void main(String[] args) {
30-
// 资源类
31-
ShareData shareData = new ShareData();
32-
33-
// 子线程 实现了Runnable接口的,lambda表达式
34-
new Thread(() -> {
35-
36-
System.out.println(Thread.currentThread().getName() + "\t come in");
37-
38-
// 线程睡眠3秒,假设在进行运算
39-
try {
40-
TimeUnit.SECONDS.sleep(3);
41-
} catch (InterruptedException e) {
42-
e.printStackTrace();
43-
}
44-
// 修改number的值
45-
shareData.setNumberTo100();
46-
47-
// 输出修改后的值
48-
System.out.println(Thread.currentThread().getName() + "\t update number value:" + shareData.number);
49-
50-
}, "子线程").start();
51-
52-
while(shareData.number == 0) {
53-
// main线程就一直在这里等待循环,直到number的值不等于零
54-
}
55-
56-
// 按道理这个值是不可能打印出来的,因为主线程运行的时候,number的值为0,所以一直在循环
57-
// 如果能输出这句话,说明子线程在睡眠3秒后,更新的number的值,重新写入到主内存,并被main线程感知到了
58-
System.out.println(Thread.currentThread().getName() + "\t 主线程知道了 number 不等于 0");
59-
60-
/**
61-
* 最后输出结果:
62-
* 子线程 come in
63-
* 子线程 update number value:100
64-
* 最后线程没有停止,并行没有输出 mission is over 这句话,说明没有用volatile修饰的变量,是没有可见性
65-
*/
66-
}
67-
}
68-
69-
1+
package com.jackson0714.passjava.threads;
2+
import java.util.concurrent.TimeUnit;
3+
/**
4+
* Volatile Java虚拟机提供的轻量级同步机制
5+
*
6+
* 可见性(及时通知)
7+
* 不保证原子性
8+
* 禁止指令重排
9+
* @author: 悟空聊架构
10+
* @create: 2020-08-12
11+
*/
12+
13+
14+
class ShareData1 {
15+
//volatile 修饰的关键字,是为了增加多个线程之间的可见性,只要有一个线程修改了内存中的值,其它线程也能马上感知
16+
volatile int number = 0;
17+
18+
public void setNumberTo100() {
19+
this.number = 100;
20+
}
21+
}
22+
23+
/**
24+
* 验证volatile的可见性
25+
* 1. 假设int number = 0, number变量之前没有添加volatile关键字修饰,没有可见性
26+
* 2. 添加了volatile,可以解决可见性问题
27+
*/
28+
public class VolatileVisibility {
29+
public static void main(String[] args) {
30+
// 资源类
31+
ShareData1 shareData = new ShareData1();
32+
System.out.println(System.identityHashCode(shareData.number));
33+
34+
// 子线程 实现了Runnable接口的,lambda表达式
35+
new Thread(() -> {
36+
37+
System.out.println(Thread.currentThread().getName() + "\t come in");
38+
39+
// 线程睡眠3秒,假设在进行运算
40+
try {
41+
TimeUnit.SECONDS.sleep(3);
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
// 修改number的值
46+
System.out.println(System.identityHashCode(shareData.number));
47+
shareData.setNumberTo100();
48+
49+
// 输出修改后的值
50+
System.out.println(Thread.currentThread().getName() + "\t update number value:" + shareData.number);
51+
52+
}, "子线程").start();
53+
54+
while(shareData.number == 0) {
55+
// main线程就一直在这里等待循环,直到number的值不等于零
56+
}
57+
58+
// 按道理这个值是不可能打印出来的,因为主线程运行的时候,number的值为0,所以一直在循环
59+
// 如果能输出这句话,说明子线程在睡眠3秒后,更新的number的值,重新写入到主内存,并被main线程感知到了
60+
System.out.println(Thread.currentThread().getName() + "\t 主线程知道了 number 不等于 0");
61+
62+
/**
63+
* 最后输出结果:
64+
* 子线程 come in
65+
* 子线程 update number value:100
66+
* 最后线程没有停止,并行没有输出 mission is over 这句话,说明没有用volatile修饰的变量,是没有可见性
67+
*/
68+
}
69+
}
70+
71+

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.