Is there any reason to check if singleton is zero twice?

I came across some code where the developer constantly checks to see if singleton is null twice with if-like in the code below:

private static processManager singleton = null; 

...

 public synchronized static processManager getInsatnce() throws Exception { if(singleton == null) { if(singleton == null){ singleton = new processManager(); } } return singleton } 

I see no reason why this could be, but there are many instances in the code, so I thought there might be a reason?

+6
source share
5 answers

Your code is not demonstrating properly. This is due to a double idiom, where it makes sense:

 // Double-check idiom for lazy initialization of instance fields. private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; } 

Read about it here .

Be careful that this idiom is a good choice only for instance fields. In your question, you have a static field, for which the simplest idiom is the main choice: the lazy identifier of the owner class:

 // Lazy initialization holder class idiom for static fields private static class FieldHolder { static final FieldType field = computeFieldValue(); } static FieldType getField() { return FieldHolder.field; } 
+15
source

I think you mean Double Checked Locking . This pattern avoids synchronization when not required.

The code should be

 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; } 

So, you see that we are only synchronized, when we checked that singleton is not null, we then double-check it in case someone has already started building it. Not for this to work singleton must be volatile . Here 's an article explaining the subtle issue that occurs if you forget volatile .

In your case, when the method is synchronized, you are right. It makes no sense to double check.

+2
source

This is an unsuccessful attempt to achieve a Double Verified Block ID . The first null check is to see if the instance has already been created and if it is not null , then simply returns the already created instance.

But checking the state is a check-and-action situation and is not thread safe , so there is a chance that two or more threads will see the value as null and create two instances of a singleton and thats doubleton, or possibly ManyTon.

So we use synchronized so that only one thread enters this block and only one instance is created.

+2
source

This makes no sense if singleton is not a property that instantiates the getter, but even then it makes no sense, then the rest of the code will be inaccessible.

0
source

This method is called double check.

However, the code you entered is incorrect. You are right, it makes no sense to double check the zero value in this way. I would say that the correct double check implementation is as follows:

 private static volatile ProcessManager instance = null; public static ProcessManager getInstance() { if (instance == null) { synchronized(ProcessManager.class) { if (instance == null) { instance = new ProcessManager(); } } } return instance; } 

Note that the second zero check is synchronized in the ProcessManager.class object. This is necessary because the getInstance () method is static.

0
source

All Articles