RSS

Singleton Design Pattern and Double-Checked Locking

23 Feb

Hi folks,

Today i am posting some interesting topic on Singleton design pattern and Double-Checked Locking concepts. Below is the detailed post.

The singleton pattern is one of the simplest java design pattern. It involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance, in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

Main Objective:

1. Ensure that only one instance of a class is created.
2. Provide a global point of access to the object.

Examples : Logger Classes, Configuration Classes, Accesing resources in shared mode and Factories implemented as Singletons.

Singleton Code :

public class Singleton {

private static Singleton instance;

private Singleton() {
// TODO Auto-generated constructor stub
}

public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

Double-Checked Locking

In the above mentioned sample code, works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation.

In an effort to make this method more efficient, an idiom called Double-Checked Locking was came in to the picture and created. The idea is to avoid the costly synchronization for all invocations of the method. The cost of synchronization differs from JVM to JVM. In the early days, the cost could be quite high. As more advanced JVMs have emerged, the cost of synchronization has decreased, but there is still a performance penalty for entering and leaving a synchronized method or block. Regardless of the advancements in JVM technology, programmers never want to waste processing time unnecessarily.

Best Singleton Code

private static Singleton instance;

private Singleton() {
// TODO Auto-generated constructor stub
}

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

Below are some reference URLS to know more information about Double-Checked Locking.

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

http://www.coderanch.com/t/233962/threads/java/Double-checked-locking-Singleton

Happy Learning!.

Have a nice day.:-)

Get glued to know more updates.

Advertisements
 
Leave a comment

Posted by on February 23, 2013 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: