1、簡介
雙重檢查鎖定(也叫做雙重檢查鎖定優(yōu)化)是一種軟件設計模式。
它的作用是減少延遲初始化在多線程環(huán)境下獲取鎖的次數,尤其是單例模式下比較突出。
- 軟件設計模式:解決常用問題的通用解決方案。編程中針對一些常見業(yè)務固有的模版。
- 延遲初始化:在編程中,將對象的創(chuàng)建,值計算或其他昂貴過程延遲到第一次使用時進行。
- 單例模式:在一定范圍內,只生成一個實例對象。
2、Java中的雙重檢查鎖定
單例模式我們需保證實例只初始化一次。
下面例子在單線程環(huán)境奏效,多線程環(huán)境下會有線程安全問題(instance被初始化多次)。
private static Singleton instance; public static Singleton getInstance() { if (null == instance) { instance = new Singleton(); } return instance; }
下面例子主要是性能問題。首先加鎖操作開銷很大,因為線程安全發(fā)生在對象初始化,而這里做了做了全局控制,造成浪費。
public synchronized static Singleton getInstance() { if (null == instance) { instance = new Singleton(); } return instance; }
為了控制線程安全又能保證性能,雙重檢查鎖定模式出現。
public static Singleton getInstance() { if (null == instance) { synchronized (Singleton.class) { if (null == instance) { instance = new Singleton(); } } } return instance; }
邏輯如下:
我們分析一下執(zhí)行邏輯:
假設有三個線程 T1 T2 T3 ,依次訪問 getInstance 方法。
- T1 第一次檢查為Null 進入同步塊,T1持有鎖,第二次檢查為Null 執(zhí)行對象創(chuàng)建。
- T2 第一次檢查為Null 進入同步塊,T2等待T1釋放鎖,鎖釋放后,T2進入執(zhí)行第二次檢查不為Null,返回實例對象。
- T3 第一次檢查不為Null,直接返回對象。
上面一切似乎很完美,但是這里面存在陷阱。根據Java內存模型我們知道,編譯器優(yōu)化處理會進行重排序。
instance = new Singleton() 大體分兩個步驟;
- 1 創(chuàng)建初始化對象;
- 2 引用賦值。
而 1 2 步驟可能顛倒,會造成對象屬性在初始化前調用的錯誤。
private static Singleton instance; ... instance = new Singleton(); ... public class Singleton { private int age; public Singleton() { this.age = 80; } }
這種細微的錯誤不容易出現,但是它的確存在。大家可以參考下面這份報告,里面詳細記錄這個問題。
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
3、列舉方案
報告里面也列舉了幾種解決方案
3.1 利用 ThreadLocal
private static final ThreadLocal<Singleton> threadInstance = new ThreadLocal<>(); public static Singleton getInstance() { if (null == threadInstance.get()) { createInstance(); } return instance; } private static void createInstance() { synchronized (Singleton.class) { if (instance == null) instance = new Singleton(); } threadInstance.set(instance); }
3.2 利用volatile(解決重排序問題)
private volatile static Singleton instance; public static Singleton getInstance() { if (null == instance) { synchronized (Singleton.class) { if (null == instance) { instance = new Singleton(); } } } return instance; }
下面是不同方案下的性能比較報告
http://www.cs.umd.edu/~pugh/java/memoryModel/DCL-performance.html
4、總結
本章節(jié)主要記錄了雙重檢查鎖定模式使用中應該注意的細微事項。
請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.onlythinking.com/