Skip to content

Commit a5261eb

Browse files
authored
Synchronization of getInstance might fail?
Hey everyone, I found this getInstance method generating a static singleton. I think the second `result = instance` should be moved into the synchronized block. Otherwise, this might result in multiple instances. One question, is it necessary to use a local variable here? (I mean it does not hurt either) This could also be changed to: ```java public static IChemObjectBuilder getInstance() { if (instance == null) { synchronized (LOCK) { if (instance == null) { instance = new SilentChemObjectBuilder(); } } } return instance; } ```
1 parent 84894c8 commit a5261eb

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

base/silent/src/main/java/org/openscience/cdk/silent/SilentChemObjectBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ public void modify(DoubleBondStereochemistry instance) {
220220
public static IChemObjectBuilder getInstance() {
221221
IChemObjectBuilder result = instance;
222222
if (result == null) {
223-
result = instance;
224223
synchronized (LOCK) {
224+
result = instance;
225225
if (result == null) {
226226
instance = result = new SilentChemObjectBuilder();
227227
}

0 commit comments

Comments
 (0)