Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 543d060

Browse files
更新volatile
1 parent 8b340f1 commit 543d060

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.javaedge.concurrency.common.volatiletest;
2+
3+
/**
4+
* @author JavaEdge
5+
* @date 2019年10月17日
6+
*/
7+
import java.util.concurrent.TimeUnit;
8+
9+
// 1、 jre/bin/server 放置hsdis动态链接库
10+
// 测试代码 将运行模式设置为-server, 变成死循环 。 没加默认就是client模式,就是正常(可见性问题)
11+
// 2、 通过设置JVM的参数,打印出jit编译的内容 (这里说的编译非class文件),通过可视化工具jitwatch进行查看
12+
// -server -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:+LogCompilation -XX:LogFile=jit.log
13+
// 关闭jit优化-Djava.compiler=NONE
14+
15+
public class VisibilityDemo {
16+
17+
/**
18+
* 运行标志
19+
*/
20+
public boolean flag = true;
21+
22+
/**
23+
* JIT just in time
24+
* @param args
25+
* @throws InterruptedException
26+
*/
27+
public static void main(String[] args) throws InterruptedException {
28+
VisibilityDemo demo1 = new VisibilityDemo();
29+
System.out.println("代码开始了");
30+
Thread thread1 = new Thread(new Runnable() {
31+
@Override
32+
public void run() {
33+
int i = 0;
34+
// boolean f = demo1.flag; 不加valo是的jvm优化
35+
// if(f) {
36+
// while(true){
37+
// i++;
38+
// }
39+
// }
40+
while (demo1.flag) {
41+
synchronized (this) {
42+
i++;
43+
}
44+
}
45+
System.out.println(i);
46+
}
47+
});
48+
thread1.start();
49+
50+
TimeUnit.SECONDS.sleep(2);
51+
// 设置is为false,使上面的线程结束while循环
52+
demo1.flag = false;
53+
System.out.println("被置为false了.");
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.javaedge.concurrency.common.volatiletest;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author JavaEdge
7+
* @date 2019年10月17日
8+
*/
9+
public class VisibilityDemo1 {
10+
11+
/**
12+
* 状态标识
13+
*/
14+
private static boolean is = true;
15+
16+
public static void main(String[] args) {
17+
new Thread(new Runnable() {
18+
@Override
19+
public void run() {
20+
int i = 0;
21+
while (VisibilityDemo1.is) {
22+
synchronized (this) {
23+
i++;
24+
}
25+
}
26+
System.out.println(i);
27+
}
28+
}).start();
29+
30+
try {
31+
TimeUnit.SECONDS.sleep(2);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
// 设置is为false,使上面的线程结束while循环
36+
VisibilityDemo1.is = false;
37+
System.out.println("被置为false了.");
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.javaedge.concurrency.common.volatiletest;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author JavaEdge
7+
* @date 2019年10月17日
8+
*/
9+
public class VisibilityDemo2 {
10+
11+
/**
12+
* 状态标识 (不用缓存)
13+
*/
14+
private volatile boolean flag = true;
15+
16+
// 源码 -> 字节码class
17+
18+
/**
19+
* JVM 转化为 操作系统能够执行的代码 (JIT Just In Time Compiler 编译器 )(JVM -- client , --server)
20+
*
21+
* @param args
22+
* @throws InterruptedException
23+
*/
24+
public static void main(String[] args) throws InterruptedException {
25+
VisibilityDemo2 demo1 = new VisibilityDemo2();
26+
new Thread(new Runnable() {
27+
@Override
28+
public void run() {
29+
int i = 0;
30+
while (demo1.flag) {
31+
i++;
32+
}
33+
System.out.println(i);
34+
}
35+
}).start();
36+
37+
TimeUnit.SECONDS.sleep(2);
38+
// 设置is为false,使上面的线程结束while循环
39+
demo1.flag = false;
40+
System.out.println("被置为false了.");
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /