Please clarify my queries regarding Singleton and Multithreading:
- What is the best way to implement Singleton in Java, in a multithreaded environment?
- What happens when multiple threads try to access
getInstance()
method at the same time? - Can we make singleton's
getInstance()
synchronized
? - Is synchronization really needed, when using Singleton classes?
Yes, it is necessary. There are several methods you can use to achieve thread safety with lazy initialization:
Draconian synchronization:
This solution requires that every thread be synchronized when in reality only the first few need to be.
Double check synchronization:
This solution ensures that only the first few threads that try to acquire your singleton have to go through the process of acquiring the lock.
Initialization on Demand:
This solution takes advantage of the Java memory model's guarantees about class initialization to ensure thread safety. Each class can only be loaded once, and it will only be loaded when it is needed. That means that the first time
getInstance
is called,InstanceHolder
will be loaded andinstance
will be created, and since this is controlled byClassLoader
s, no additional synchronization is necessary.Refer to this post for best way to implement Singleton.
What is an efficient way to implement a singleton pattern in Java?
It depends on the way you have implemented the method.If you use double locking without volatile variable, you may get partially constructed Singleton object.
Refer to this question for more details:
Why is volatile used in this example of double checked locking
Not required if you implement the Singleton in below ways
Refer to this question fore more details
Java Singleton Design Pattern : Questions
If you are working on a multithreaded environment in Java and need to gurantee all those threads are accessing a single instance of a class you can use an Enum. This will have the added advantage of helping you handle serialization.
and then just have your threads use your instance like:
Enum singleton
The simplest way to implement a Singleton that is thread-safe is using an Enum
This code works since the introduction of Enum in Java 1.5
Double checked locking
If you want to code a “classic” singleton that works in a multithreaded environment (starting from Java 1.5) you should use this one.
This is not thread-safe before 1.5 because the implementation of the volatile keyword was different.
Early loading Singleton (works even before Java 1.5)
This implementation instantiates the singleton when the class is loaded and provides thread safety.
Yes, you need to make
getInstance()
synchronized. If it's not there might arise a situation where multiple instances of the class can be made.Consider the case where you have two threads that call
getInstance()
at the same time. Now imagine T1 executes just past theinstance == null
check, and then T2 runs. At this point in time the instance is not created or set, so T2 will pass the check and create the instance. Now imagine that execution switches back to T1. Now the singleton is created, but T1 has already done the check! It will proceed to make the object again! MakinggetInstance()
synchronized prevents this problem.There a few ways to make singletons thread-safe, but making
getInstance()
synchronized is probably the simplest.This pattern does a thread-safe lazy-initialization of the instance without explicit synchronization!
It works because it uses the class loader to do all the synchronization for you for free: The class
MySingleton.Loader
is first accessed inside thegetInstance()
method, so theLoader
class loads whengetInstance()
is called for the first time. Further, the class loader guarantees that all static initialization is complete before you get access to the class - that's what gives you thread-safety.It's like magic.
It's actually very similar to the enum pattern of Jhurtado, but I find the enum pattern an abuse of the enum concept (although it does work)