How singleton pattern works in java

Singleton pattern is a design solution where an application wants to have one and only one instance of any class, in all possible scenarios without any exceptional condition.

There are several ways to implement the singleton pattern. The following sections will give an idea how singleton pattern is implemented.
Eager initialization

In this pattern an instance of a class is created much before it is actually required. Mostly it is done on system start up.

 

It works fine, but has one performance drawback. The getInstance() method is synchronized and each call will require extra locking/unlocking steps which are necessary only for first time, and never there after.

Lazy initialization

In singleton pattern, it restricts the creation of instance until required first time.

 

At first request, the above method ckecks whether an instance created or not. If there is no instance then it will create an instance and will return its reference. If instance was already created, it will simply return the reference of the existing instance.

But, it has also drawbacks. Suppose there are two or more threads and all comes to create instance and execute “instance==null”, now all threads have identified instance variable to null thus assume they must create an instance. They sequentially goes to synchronized block and create the instances. At the end, we have several instances in our application.

This error can be solved using double-check locking.

 

Static block initialization

static blocks are executed during the loading of class and even before the constructor is called. This feature is also used in singleton pattern.

 

Above code has one drawback. One instance will be created irrespective of whether we require it or not.

Next section will overcome this problem.

Using Enum

Enum provides implicit support for thread safety and only one instance is guaranteed.

 

Adding readResolve()

Even there are several ways of implementing singleton pattern but still it may arise some problems. For example, there is an application which is distributed and frequently serializes the objects in file system. Only read those objects at later time when required.

 

Let’s serialize and de-serialize it after making some changes:

 

Output:

 

As we see both variables have different value. So it means that there are two instances of the class.

To solve this issue, we need to include readResolve() method in our SingletonDemo class. This method will be invoked when you will de-serialize the object. Inside this method, we must return the existing instance to ensure single instance application wide.

 

Now it will give the correct output.

 

Thanks for your reading. Please leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.