当前位置:网站首页>『每日一问』怎么实现一个正确的双重检查锁定
『每日一问』怎么实现一个正确的双重检查锁定
2022-07-25 09:16:00 【高级摸鱼工程师】
缘由
我们程序里,有时候一些开销比较大的对象创建,往往不会提前创建,而是只有在实际要用到的时候才会去创建。
也就是基本下边这个写法:
public class UnsafeLazyInitialization {
private static Instance instance;
public static Instance getInstance() {
if (instance == null) // 1:A线程执行
instance = new Instance(); // 2:B线程执行
return instance;
}
}
上边这个写法呢,在A线程执行到1的时候,B线程刚执行完2。那么这时候A线程可能看到instance还没有完成初始化。那么就产生的内存可见性问题。
劳动人民的智慧是无穷的 “加个synchronized完事了”~~
public class UnsafeLazyInitialization {
private static Instance instance;
public synchronized static Instance getInstance() {
if (instance == null)
instance = new Instance();
return instance;
}
}
但是这个写法吧,如果并发线程很多,那么锁的频繁竞争就会导致性能开销很大。因为在早期的JVM中,synchronized性能比较差,所以劳动人民的智慧又发出了炽热的光芒闪瞎了JVM的狗眼~~
双重检查锁定(Double-Checked Locking)横空出世!!!
public class DoubleCheckedLocking {
// 1
private static Instance instance; // 2
public static Instance getInstance() {
// 3
if (instance == null) {
// 4:第一次检查
synchronized (DoubleCheckedLocking.class) {
// 5:加锁
if (instance == null) // 6:第二次检查
instance = new Instance(); // 7:问题的根源出在这里
} // 8
} // 9
return instance; // 10
} // 11
}
按照上边的这个写法,在第一次检查的时候,如果对象不为null,那么就直接返回对象实例。如果对象为null,那么就加锁去实例化对象。也就保证了多线程下只有一个线程可以去实例化对象。
看起来很完美,但其实还是老问题,线程A在判断instance不为null之后获取到的instance可能还没有初始化完成。
那么为什么会出现这种情况呢?
根源
一个对象的创建可以大概概括为三个步骤: TODO:不仅仅是这三步,后面会出文章专门补充
- 分配内存空间
- 初始化对象
- 设置对象指向分配的内存空间
问题就出在上面的第二步和第三步可能会出现重排序。
Java语言规范[The Java Language Specification]中要求,所有线程在执行Java程序时必须遵守intra-thread semantics(线程内语义),intra-thread semantics允许那些在单线程内不会改变执行结果的重排序。为啥要允许呢,因为可以提高程序的执行性能呗~
上述时序图中,A2和A3虽然重排序了,但是按照Java内存模型的intra-thread semantics将确保A4排在A2前面。只有这样A线程的执行结果才不会改变。但是因为A2、A3的重排将导致B1在判断instance是为为null的时候返回false,因为这个时候A3已经执行完了。所以呢,B2直接访问对象的时候就会出问题了,毕竟这个对象还没有初始化完毕呢。
解决方案
知道了问题产生的原因,那么相应的解决思路也就很顺滑的出来了是吧
- 禁止A2、A3重排
- A2、A3重排的时候,对别的线程不可见
public class DoubleCheckedLocking {
// 1
private volatile static Instance instance; // 2 : 关键字volatile
public static Instance getInstance() {
// 3
if (instance == null) {
// 4 --- 线程A
synchronized (DoubleCheckedLocking.class) {
// 5
if (instance == null) // 6
instance = new Instance(); // 7 --- 线程B
}
}
return instance;
}
}
第一种思路的可以使用关键字volatile来实现。『每日一问』volatile干嘛的
通过volatile的内存语义我们可以知道,在线程A执行到第四行的时候,如果线程B在第七行的对象创建没有完成,则线程B不会刷新主内存,那么线程A就不会出现instance的内存可见性问题。
······································································
public class InstanceFactory {
private static class InstanceHolder {
public static Instance instance = new Instance();
}
public static Instance getInstance() {
return InstanceHolder.instance ; // 这里将导致InstanceHolder类被初始化
}
}
第二种思路可以借助类初始化的思路去实现。
每一个类在初始化的时候都需要去获取一个“锁[其实是对象头中的一个状态值]”,只有获取这个“锁”的线程才可以执行对象的初始化工作。这里需要说一点,一个类的静态变量被赋值,会触发类的初始化~~
右上我们可以看到,借用类初始化的特性,我们避免了一个对象在尚未初始化完成之前被别的线程使用而导致的内存可见性问题~
边栏推荐
猜你喜欢

Robot jumping problem

canvas很多圆圈组成的文本js特效

Opencv realizes simple face tracking

Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32

Sticky.js page scrolling div fixed position plug-in
![[BUUCTF-n1book][第二章 web进阶]SSRF Training](/img/29/8894d04b27e0e73c4458c27bd9b935.png)
[BUUCTF-n1book][第二章 web进阶]SSRF Training

mysql中的数据结果排名

sql注入
![[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box](/img/5c/52ccc0583451eba15fec18adb1499e.png)
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box
![[STL]list模拟实现](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[STL]list模拟实现
随机推荐
[common tools] obtain system status information based on psutil and gputil
【线程知识点】-- 自旋锁
Arrange the array into the smallest number
【sklearn】sklearn.preprocessing.LabelEncoder
The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
Additional: SQL statement area / county in the lower half (data table)
图解LeetCode——919. 完全二叉树插入器(难度:中等)
Silicon Valley class lesson 11 - official account news and wechat authorization
『怎么用』代理模式
c语言中的六个存储类型:auto register static extern const volatile
activemq--持久化机制之LevelDB
API健康状态自检
table表格展开内部行切换效果
ActiveMQ -- kahadb of persistent mechanism
超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张
[arm] Xintang nuc977 transplants wk2124 drive
Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
The hole of scroll view in uniapp
Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)
nacos2.1.0集群搭建