关于 previous question我提出,
public static Singleton getInstanceDC() {
if (_instance == null) { // Single Checked (1)
synchronized (Singleton.class) {
if (_instance == null) { // Double checked (2)
_instance = new Singleton();
}
}
}
return _instance;
为什么要使用第二个实例空检查条件。它可能产生什么影响?
最佳答案
让我们对行进行编号,以便了解线程如何交错操作。
if (_instance == null) { // L1
synchronized (Singleton.class) { // L2
if (_instance == null) { // L3
_instance = new Singleton();// L4
}
}
}
让我们考虑一个不检查 L3 的交错。
- 线程 1 到达 L1 且
_instance
为null
- 线程 2 到达 L1 且
_instance
为null
- 线程 1 在 L2 获得互斥锁
- 线程 2 试图在 L2 处获取互斥量但阻塞了
- 线程 1 创建新实例并在 L4 分配
- 线程 1 从 L2 释放互斥锁
- 线程 2 在 L2 获得互斥锁
- 线程 2 创建新实例并在 L4 分配
- 线程 2 从 L2 释放互斥量
Singleton
创建了两个实例。每个线程返回自己的实例。
通过 L3 的检查,第 8 步不会发生,因为在第 7 步线程 2 的 _instance
View 与线程 1 的 View 同步,因此只有一个 Singleton
实例已创建。
关于java - 为什么要使用双重检查锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427986/