Double Checked Locking in Singleton

JavaMultithreadingDesign PatternsSingleton

Java Problem Overview


here is my custom class for singleton pattern. in this code, I use double-checked locking as below. As I read many posts on some source, they say that double check is useful because it prevents two concurrent threads run at same times make two different objects.

public class DoubleCheckLocking {

	public static class SearchBox {
		private static volatile SearchBox searchBox;
		
		// private constructor
		private SearchBox() {}
		
		// static method to get instance
		public static SearchBox getInstance() {
			if (searchBox == null) { // first time lock
				synchronized (SearchBox.class) {
					if (searchBox == null) {  // second time lock
						searchBox = new SearchBox();
					}
				}
			}
			return searchBox;
		}
}

I still don't understand above code so much. What is the problem, if two threads together run same line of code when instance is null ?

if (searchBox == null) {
				synchronized (SearchBox.class) {
					if (searchBox == null) {
						searchBox = new SearchBox();
					}
				}
			}

When that appear. both two threads will see object is null. then both synchronize. and then, they check again, and still see it null. and create two different objects. OOOPS.

Please explain for me. What have I understand wrong ?

Thanks :)

Java Solutions


Solution 1 - Java

No, since you are obtaining lock on the SearchBox.class, only one thread will enter the synchronized block at a time. So the first thread enters then finds searchBox is null and creates it and then leaves the synchronized block, then the second thread enter the block then it finds that the searchBox is not null because the first thread already created it so it will not create a new instance of searchBox.

The double checked pattern is used to avoid obtaining the lock every time the code is executed. If the call are not happening together then the first condition will fail and the code execution will not execute the locking thus saving resources.

Solution 2 - Java

Let's look at this code:

1 if (searchBox == null) {
2     synchronized (SearchBox.class) {
3     if (searchBox == null) {
4         searchBox = new SearchBox();
5     }
6 }

Let's try to reason about this. Let's say we have two threads A and B and let's assume that at least one of them reaches line 3 and observes searchBox == null is true. Two threads can not both be at line 3 at the same time because of the synchronized block. This is the key to understanding why double-checked locking works. So, it must the case that either A or B made it through synchronized first. Without loss of generality, say that that thread is A. Then, upon seeing searchBox == null is true, it will enter the body of the statement, and set searchBox to a new instance of SearchBox. It will then eventually exit the synchronized block. Now it will be B's turn to enter: remember, B was blocked waiting for A to exit. Now when it enters the block, it will observe searchBox. But A will have left just having set searchBox to a non-null value. Done.

By the way, in Java, the best way to implement a singleton is to use a single-element enum type. From Effective Java:

>While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

Solution 3 - Java

This double check lock is only necessary if you are worried about many threads calling the singleton simultaneously, or the cost of obtaining a lock in general.

Its purpose is to prevent unnecessary synchronization, thereby keeping your code fast in a multi-threaded environment.

Check out this link for more information.

If you are running in Java 1.5 or greater, and you use the volatile keyword in your double-check locked mechanism, it will work fine. As you are using the volatile keyword, your example is not broken according to the same link above.

Solution 4 - Java

if (searchBox == null) { //1
    synchronized (SearchBox.class) {
        if (searchBox == null) {  //2
            searchBox = new SearchBox();
            }
        }
    }
}
  1. If an instance was already created, don't do anything - avoid locking threads
  2. The first thread that has acquired the lock checks and sees that there is no such object and creates it. It releases the lock and the second one can do the same - it has to check if the object exists because the first one may have created it.

So basically the outer if is used to prevent redundant locks - it lets all thread know that there is already an object and they don't need to lock/do anything. And the inner if is used to let a concurrent thread know whether another has already created the object or not.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhqtView Question on Stackoverflow
Solution 1 - JavaArun P JohnyView Answer on Stackoverflow
Solution 2 - JavajasonView Answer on Stackoverflow
Solution 3 - JavaWilliam MorrisonView Answer on Stackoverflow
Solution 4 - Javastan0View Answer on Stackoverflow