Skip to content

Commit 708b400

Browse files
JMM
1 parent 2cdd584 commit 708b400

23 files changed

+507
-2
lines changed

JMM/src/JVM.png

420 KB
Loading

JMM/src/JmmSummary.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class JmmSummary {
1212
*
1313
* Java内存模型,和Java的并发编程有关
1414
* -> 实际上是一组规范,需要各个JVM的实现来遵守JMM规范,以便于开发者可以利用这些规范,更方便地开发多线程程序
15-
* -> JVM是工具类和关键字的原理,volatile、synchronized、lock等原理都是JVM
16-
* -> 没有JVM,就需要自己指定什么时候用内存栅栏(如工作内存和主内存之间的拷贝与同步)
15+
* -> JMM是工具类和关键字的原理,volatile、synchronized、lock等原理都是JMM
16+
* -> 没有JMM,就需要自己指定什么时候用内存栅栏(如工作内存和主内存之间的拷贝与同步)
1717
*
1818
* Java对象模型,和Java对象在虚拟机中的表现形式有关
1919
* -> 栈,堆,方法区
@@ -41,4 +41,14 @@ public class JmmSummary {
4141
* 所有的共享变量存在于主内存中,每个线程有自己的本地内存,而且线程读写共享数据也是通过本地内存交换的,所有才导致了可见性问题
4242
*/
4343

44+
/**
45+
* happens-before原则 -> 保证可见性的措施
46+
*/
47+
48+
49+
/**
50+
* 饿汉式 -> 没有延迟加载
51+
* 懒汉式 -> 可能无法保证线程安全问题
52+
*/
53+
4454
}

JMM/src/atomic/AtomicInJava.png

349 KB
Loading

JMM/src/atomic/PlusCantAtomic.png

409 KB
Loading
267 KB
Loading
176 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package outoforder;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* volatile禁止重排序
7+
*
8+
* @Author: zzStar
9+
* @Date: 10-19-2020 16:37
10+
*/
11+
public class OutOfOrderExecutionVolatile {
12+
13+
private volatile static int x = 0, y = 0;
14+
private volatile static int a = 0, b = 0;
15+
16+
public static void main(String[] args) throws InterruptedException {
17+
18+
int i = 0;
19+
for (; ; ) {
20+
i++;
21+
// 回到开始
22+
x = 0;
23+
y = 0;
24+
a = 0;
25+
b = 0;
26+
27+
28+
CountDownLatch countDownLatch = new CountDownLatch(1);
29+
30+
Thread one = new Thread(() -> {
31+
try {
32+
countDownLatch.await();
33+
} catch (InterruptedException e) {
34+
e.printStackTrace();
35+
}
36+
a = 1;
37+
x = b;
38+
});
39+
40+
Thread two = new Thread(() -> {
41+
try {
42+
countDownLatch.await();
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
b = 1;
47+
y = a;
48+
});
49+
one.start();
50+
two.start();
51+
countDownLatch.countDown();
52+
one.join();
53+
two.join();
54+
55+
String result = "No." + i + "次(" + x + "," + y + ")";
56+
if (x == 0 && y == 0) {
57+
System.out.println(result);
58+
break;
59+
} else {
60+
System.out.println(result);
61+
}
62+
}
63+
}
64+
}
366 KB
Loading
423 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package singleton.hungryman;
2+
3+
/**
4+
* 单例实现之二 -> 饿汉式(静态代码块)
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-20-2020 10:24
8+
*/
9+
public class SingletonStaticCodeBlock {
10+
11+
static {
12+
INSTANCE = new SingletonStaticCodeBlock();
13+
}
14+
15+
private final static SingletonStaticCodeBlock INSTANCE;
16+
17+
public static SingletonStaticCodeBlock getINSTANCE() {
18+
return INSTANCE;
19+
}
20+
21+
private SingletonStaticCodeBlock() {
22+
}
23+
24+
}

0 commit comments

Comments
 (0)