Static Initialization using Singleton pattern - can it solve the multi threaded environment issue instead of explicitly going for lock block.
-
it depends on so many things. – njzk2 Jun 03 '16 at 20:25
-
can you please brief me based on specific context like with a example @njzk2 – V.Sriram Jun 03 '16 at 20:26
-
if you are only reading from the singleton, or have one thread that writes and don't care about latency and reading stale data, then singleton is fine. Otherwise, you'll have issues. – njzk2 Jun 03 '16 at 20:29
-
ok.. but anyway with static initialization we will be defining the class with sealed keyword to prevent further deriving instances right ? – V.Sriram Jun 03 '16 at 20:32
-
yes, the static init makes the instance itself thread safe. – njzk2 Jun 03 '16 at 20:35
-
but then what you do with the instance itself may or may not be threadsafe – njzk2 Jun 03 '16 at 20:35
-
ok.. thank you @njzk2 for clarifying the same. – V.Sriram Jun 03 '16 at 20:37
-
side note: what I said apply to Java, I didn't realize there was no language tag attached to your question. – njzk2 Jun 03 '16 at 22:54
2 Answers
There are many ways to write a thread-safe singleton class. Let's take a look.
There are 3 things, that you might care about, when creating singleton:
- thread-safety
- throughput
- lazy loading
- ability to handle exceptions, thrown in constructor.
The most native implementation in the following:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
The only disadvantage here, is that you synchronize each time, when calling getInstance()
, and throughput will suffer, when multiple threads will call getInstance()
in the same time.
Ok, the next solution is:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public synchronized static Singleton getInstance() {
return instance;
}
It's thread safe, throughput is fine, but we lost lazy loading (instance will be created when class is loaded, but not when first getInstance()
is called) and we can't handle exceptions from constructor.
Next:
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
private final static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
This implementation is my favourite one. It's thread safe, throughput is perfect and its lazy. The only drawback is that you still have no ability to handle exceptions from constructor. Otherwise, use it.
If handling exceptions from constructor is critical, use double-check locking:
class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public Singleton getInstance() {
if (instance == null) {
synchronized(this) {
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
Note, that it can be used only since Java 5, because of volatile key-word semantics.

- 247
- 2
- 7
You can implement thread safe singleton but there is cost to pay if the object is heavily used by different thread. Thread Safe C# Singleton Pattern and http://csharpindepth.com/Articles/General/Singleton.aspx#performance