在 Effecitve Java 一書的第 48 條中提到了雙重檢查模式,并指出這種模式在 Java 中通常并不適用。該模式的結(jié)構(gòu)如下所示:
1
2
3
4
5
6
7
8
9
10
|
public Resource getResource() { if (resource == null ) { synchronized ( this ){ if (resource== null ) { resource = new Resource(); } } } return resource; } |
該模式是對下面的代碼改進(jìn):
1
2
3
4
5
6
|
public synchronized Resource getResource(){ if (resource == null ){ resource = new Resource(); } return resource; } |
這段代碼的目的是對 resource 延遲初始化。但是每次訪問的時候都需要同步。為了減少同步的開銷,于是有了雙重檢查模式。
在 Java 中雙重檢查模式無效的原因是在不同步的情況下引用類型不是線程安全的。對于除了 long 和 double 的基本類型,雙重檢查模式是適用 的。比如下面這段代碼就是正確的:
1
2
3
4
5
6
7
8
9
10
11
|
private int count; public int getCount(){ if (count == 0 ){ synchronized ( this ){ if (count == 0 ){ count = computeCount(); //一個耗時的計(jì)算 } } } return count; } |
上面就是關(guān)于java中雙重檢查模式(double-check idiom)的一般結(jié)論。但是事情還沒有結(jié)束,因?yàn)閖ava的內(nèi)存模式也在改進(jìn)中。Doug Lea 在他的文章中寫道:“根據(jù)最新的 JSR133 的 Java 內(nèi)存模型,如果將引用類型聲明為 volatile,雙重檢查模式就可以工作了”。所以以后要在 Java 中使用雙重檢查模式,可以使用下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
|
private volatile Resource resource; public Resource getResource(){ if (resource == null ){ synchronized ( this ){ if (resource== null ){ resource = new Resource(); } } } return resource; } |
當(dāng)然了,得是在遵循 JSR133 規(guī)范的 Java 中。
所以,double-check 在 J2SE 1.4 或早期版本在多線程或者 JVM 調(diào)優(yōu)時由于 out-of-order writes,是不可用的。 這個問題在 J2SE 5.0 中已經(jīng)被修復(fù),可以使用 volatile 關(guān)鍵字來保證多線程下的單例。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Singleton { private volatile Singleton instance = null ; public Singleton getInstance() { if (instance == null ) { synchronized ( this ) { if (instance == null ) { instance = new Singleton(); } } } return instance; } } |
推薦方法 是Initialization on Demand Holder(IODH),
1
2
3
4
5
6
7
8
9
|
public class Singleton { static class SingletonHolder { static Singleton instance = new Singleton(); } public static Singleton getInstance(){ return SingletonHolder.instance; } } |
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。