I was wondering about singleton in enum and it's performance.
When we have multithreaded environment we have to synchronize moment, when an instance is created.
Simply, we can use synchronized mod, for function called getInstance() which create instance Somethink like that:
public synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
It's lazy implementation it's good for me. But synchronized method is slow. We can use double-locking to make it faster.
How about enum? When we implement singleton as enum, instance of singleton will be created as first use. Next use, return current instance.
How it works? When we want to get existing instance, there is implicit synchronized method which are slow? Or there is Double-lock implemented?