シングルトンパターンにダブルチェックロックイディオムを使用する方が良いですか?または、同期メソッド?シングルトンパターンにダブルチェックロックイディオムを使用するのが最適ですか?
すなわち:
private static volatile ProcessManager singleton = null;
public static ProcessManager getInstance() throws Exception {
if (singleton == null) {
synchronized (MyClass.class) {
if (singleton == null) {
singleton = new ProcessManager();
}
}
}
return singleton;
}
または
private static processManager singleton = null;
public synchronized static processManager getInsatnce() throws Exception {
if(singleton == null) {
singleton = new processManager();
}
return singleton
}
を高めることを言うこと
注意あなたの2番目のコードのように、意味をなさない。 – Jesper
あなた自身の前の質問に[この回答](http://stackoverflow.com/a/15498689/1103872)で説明されているように、上記のどれもありません。 –
2番目の例では、すでに重大なセクションに入っているので、ダブルチェックは必要ありません(あなたのクラスのどこかでシングルトンメンバーに触れていないと仮定して) – Grimmy