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 — 更新 01.反制面试官_14张原理图_再也不怕被问volatile!.md · kings186/PassJava-Learning@a0856af

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • code/01.Threads/src/com/jackson0714/passjava/threads
    • VolatileResort.java
    • VolatileSingleton.java
  • docs/10.并发多线程
    • 01.volatile.assets
      • image-20200817102904240.png
      • image-20200817134523840.png
      • image-20200817163859759.png
    • 01.反制面试官_14张原理图_再也不怕被问volatile!.md
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 clipboardExpand all lines: code/01.Threads/src/com/jackson0714/passjava/threads/VolatileResort.java
+1-7Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,20 @@
88

99

1010
public class VolatileResort {
11-
1211
static int num = 0;
13-
static boolean flag = false;
14-
12+
static volatile boolean flag = false;
1513
public static void init() {
1614
num= 1;
1715
flag = true;
1816
}
19-
2017
public static void add() {
21-
2218
while (flag) {
2319
flag = false;
2420
num = num + 100;
2521
System.out.println("num:" + num);
2622
}
2723
}
28-
2924
public static void main(String[] args) throws InterruptedException {
30-
3125
new Thread(() -> {
3226
add();
3327
},"子线程").start();
Collapse file

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

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jackson0714.passjava.threads;
2+
/**
3+
演示volatile 单例模式应用(双边检测)
4+
* @author: 悟空聊架构
5+
* @create: 2020-08-17
6+
*/
7+
8+
class VolatileSingleton {
9+
private static volatile VolatileSingleton instance = null;
10+
private VolatileSingleton() {
11+
System.out.println(Thread.currentThread().getName() + "\t 我是构造方法SingletonDemo");
12+
}
13+
public static VolatileSingleton getInstance() {
14+
// 第一重检测
15+
if(instance == null) {
16+
// 锁定代码块
17+
synchronized (VolatileSingleton.class) {
18+
// 第二重检测
19+
if(instance == null) {
20+
// 实例化对象
21+
instance = new VolatileSingleton();
22+
}
23+
}
24+
}
25+
return instance;
26+
}
27+
}
Collapse file

‎docs/10.并发多线程/01.volatile.assets/image-20200817102904240.png‎

Copy file name to clipboard
16.7 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎docs/10.并发多线程/01.volatile.assets/image-20200817134523840.png‎

Copy file name to clipboard
48.1 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎docs/10.并发多线程/01.volatile.assets/image-20200817163859759.png‎

Copy file name to clipboard
26.3 KB
  • Display the source diff
  • Display the rich diff
Loading

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.