double-checked locking is not thread-safe in Java

found by PMD

Change-Id: Ibd4a9139c626932bec56c0b1dd32b4d59c8440b1
This commit is contained in:
Noel Grandin
2014-08-13 09:02:41 +02:00
parent a240a78cc7
commit 252ed1708a
11 changed files with 28 additions and 65 deletions

View File

@@ -36,13 +36,9 @@ public class XMLParserFactory {
private XMLParserFactory() {}
public static XMLParser getParser() {
if (parser == null) {
synchronized (XMLParserFactory.class) {
if (parser == null)
parser = new DefaultParser();
}
}
public static synchronized XMLParser getParser() {
if (parser == null)
parser = new DefaultParser();
return parser;
}

View File

@@ -94,18 +94,12 @@ public class ScriptEditorForBeanShell
/**
* Returns the global ScriptEditorForBeanShell instance.
*/
public static ScriptEditorForBeanShell getEditor()
public static synchronized ScriptEditorForBeanShell getEditor()
{
if (theScriptEditorForBeanShell == null)
{
synchronized(ScriptEditorForBeanShell.class)
{
if (theScriptEditorForBeanShell == null)
{
theScriptEditorForBeanShell =
new ScriptEditorForBeanShell();
}
}
theScriptEditorForBeanShell =
new ScriptEditorForBeanShell();
}
return theScriptEditorForBeanShell;
}

View File

@@ -82,18 +82,12 @@ public class ScriptEditorForJavaScript implements ScriptEditor
/**
* Returns the global ScriptEditorForJavaScript instance.
*/
public static ScriptEditorForJavaScript getEditor()
public static synchronized ScriptEditorForJavaScript getEditor()
{
if (theScriptEditorForJavaScript == null)
{
synchronized(ScriptEditorForJavaScript.class)
{
if (theScriptEditorForJavaScript == null)
{
theScriptEditorForJavaScript =
new ScriptEditorForJavaScript();
}
}
theScriptEditorForJavaScript =
new ScriptEditorForJavaScript();
}
return theScriptEditorForJavaScript;
}