Thread safe Singleton means a Singleton class which returns exactly same
instance even if exposed to multiple threads. Singleton in Java has been a
classical design pattern like Factory
method pattern or Decorator
design pattern and has been used a lot even inside JDK like java.lang.Runtime
is an example of Singleton class. Singleton pattern ensures that exactly
one instance of class will remain in Java program at any time. In our last post
10
Interview questions on Singleton in Java we have discussed many different
questions asked on Singleton pattern, One of them was writing Thread safe
singleton in Java. Prior to Java 5 double
checked locking mechanism is used to create thread-safe singleton in Java which breaks if one Thread doesn't
see instance created by other thread at same time and eventually you will end
up with more than one instance of Singleton class. From Java 5 onwards volatile
variable guarantee can be used to write thread safe singleton by using
double checked locking pattern. I personally don't prefer that way as there are many other
simpler alternatives to write thread-safe singleton is available available like
using static
field to initialize Singleton instance or using Enum
as Singleton in Java. Let’s see example of both ways to create thread-safe
Singleton in Java.
Java Singleton Example – Thread safe Singleton in Java using Enum
This is one of the example of Enum which I missed while writing 10
Examples of Enum in Java. Using Enum to create Singleton is by far most
simple and effective way to create thread-safe Singleton in Java, as
thread-safety guarantee is provided by Java programming language itself. You
don't need to bother about thread-safety issue. Since Enum instances are by
default final
in Java, it also provides safety against multiple instance due to serialization.
One point worth remembering is that, when we talk about thread-safe Singleton,
we are talking about thread-safety during instance creation of Singleton class
and not when we call any method of Singleton class. If your Singleton class
maintain any state and contains method to modify that state, you need to write
code to avoid and thread-safety
and synchronization
issues. Any way here is code example
of creating thread safe Singleton in Java using Enum.
public
enum Singleton{
INSTANCE;
public void show(){
System.out.println("Singleton using Enum in Java");
}
}
INSTANCE;
public void show(){
System.out.println("Singleton using Enum in Java");
}
}
//You can access this Singleton
as Singleton.INSTANCE and call any method like below
Singleton.INSTANCE.show();
If this suits your need than this is the most easy way of writing
thread-safe Singleton in Java. Using Enum as Singleton also provide couple of
more benefits which you can find out on Why
Enum Singleton are better in Java.
Java Singleton Example - Thread Safe Singleton using Static field Initialization
You can also create thread safe Singleton in Java by creating Singleton
instance during class
loading. static fields are initialized during class loading and Classloader
will guarantee that instance will not be visible until its fully created. Here
is example of creating thread safe singleton in Java using static factory
method. Only disadvantage of this implementing Singleton patter using static
field is that this is not a lazy
initialization and Singleton is initialized even before any clients call
there getInstance() method.
public class
Singleton{
private static final Singleton INSTANCE = new Singleton();
private Singleton(){ }
public static Singleton getInstance(){
return INSTANCE;
}
public void show(){
System.out.println("Singleon using static initialization in Java");
}
}
private static final Singleton INSTANCE = new Singleton();
private Singleton(){ }
public static Singleton getInstance(){
return INSTANCE;
}
public void show(){
System.out.println("Singleon using static initialization in Java");
}
}
//Here is how to access this
Singleton class
Singleton.getInstance().show();
here we are not creating Singleton instance inside getInstance() method
instead it will be created by ClassLoader. Also private
constructor makes impossible to create another instance , except one case.
You can still access private constructor by reflection and calling setAccessible(true). By the way
You can still prevent creating another instance of Singleton by this way by throwing
Exception from constructor.
That's all on how to create thread safe Singleton in Java. Both
the approach are safe with thread-safety issue but my personal favorite is
using Enum because of its simplicity, prevention of multiple instance against
Serialization attack and concise code.
Other Java design pattern tutorials from Javarevisited Blog
10 comments :
public class Singleton also needs to be declared final, to avoid being subclassed (and, for example, instantiate a new Singleton object via reflection)
@Anonymous, does it really necessary to make Singleton final, doesn't making constructor private will prevent it from being extended? Regarding Reflection, I think only way to prevent creating instance is throwing Exception from constructor itself.
How about static inner class with singleton instance?
Hi, and Happy New Year
Showed singleton is only one of more thread-safe singleton realizations.
Each of which have own pros and cons.
Actually this have one problem only if you have...
Imagin you have huge entrprise platfrom that have several class loaders. Each class loader work simultaneously. In this case, class loaders can create several singleton instances from only one class.
Writing a thread safe singleton for multithreaded environment is not a joke. Anything with threading requires serious knowledge of Java multithreading concept. I like your blog for spreading words and experience on Java Singleton multithreading issues.
If you guys are looking for thread safety and the combination of lazy initialization..then follow Initialization-on-demand holder idiom..
public class Singleton {
private Singleton() {
}
private static class LazyHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}
The implementation relies on the well-specified initialization phase of execution within the Java Virtual Machine (JVM); see section 12.4 of Java Language Specification (JLS) for details.
When the class Something is loaded by the JVM, the class goes through initialization. Since the class does not have any static variables to initialize, the initialization completes trivially. The static class definition LazyHolder within it is not initialized until the JVM determines that LazyHolder must be executed. The static class LazyHolder is only executed when the static method getInstance is invoked on the class Something, and the first time this happens the JVM will load and initialize the LazyHolder class. The initialization of the LazyHolder class results in static variable INSTANCE being initialized by executing the (private) constructor for the outer class Something. Since the class initialization phase is guaranteed by the JLS to be serial, i.e., non-concurrent, no further synchronization is required in the static getInstance method during loading and initialization. And since the initialization phase writes the static variable INSTANCE in a serial operation, all subsequent concurrent invocations of the getInstance will return the same correctly initialized INSTANCE without incurring any additional synchronization overhead.
Why Singleton is not thread-safe? Sorry but I didn't get the problem with Singleton and multiple threads. Can you please explain me in Simple words.
as per given example... corrected class should be like below
package org.tutorial.designpattern.singleton;
public final class SingletonClass {
private static SingletonClass singletonClass = new SingletonClass();
private SingletonClass(){}
public static SingletonClass getInstance(){
return singletonClass;
}
public int showHashCode(){
return singletonClass.hashCode();
}
}
For the following code
public enum SingletonConnection
{
INSTANCE;
private Connection conn;
private SingletonConnection ()
{
//... some code heavy opetation
//. some code heavy operation
// come Code heavy operation
//conn = ConstructConnection(); heavy operation
}
public Connection getConnection()
{
return connection;
}
}
Basically I want to have heavy Enum Constant
Now under the following scenario how jvm guarantees that no two connection objects are initialized;
If two threads access SingletonConnection.INSTANCE, where we get a case where two threads executing constructor code at same point of time and will two Connection Objects are initialize?. If so how to avoid.
Basically I want to have heavy Enum Constant which is a heavy operation. How can I can I acieve
Enum constants are created by JVM and this is a single threaded operation guaranteed by JVM, so you don't need to worry about it. That's why Enum Singletons are better than normal one because you don't have to do anything special to guarantee that thread-safe creation of Singleton.
Post a Comment