Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Checked Locking in Singleton

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 :)

like image 278
hqt Avatar asked Aug 07 '13 02:08

hqt


People also ask

What is double-checked locking of singleton?

Double checked locking of Singleton is a way to make sure that only one instance of Singleton class is created through an application life cycle.

Why you need double-checked locking of singleton class?

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.

Is double-checked locking good?

Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment. Unfortunately, it will not work reliably in a platform independent way when implemented in Java, without additional synchronization.

Is double-checked locking thread safe?

Double-checked locking is a common pattern for lazy initialization of a field accessed by multiple threads.


1 Answers

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.

like image 195
Arun P Johny Avatar answered Sep 18 '22 10:09

Arun P Johny