Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save surajdubey/c5cfa215c894364951890047b664826d to your computer and use it in GitHub Desktop.

Select an option

Save surajdubey/c5cfa215c894364951890047b664826d to your computer and use it in GitHub Desktop.
class ThreadSafeSingletonWithDoubleCheckedLocking {
private static ThreadSafeSingletonWithDoubleCheckedLocking threadSafeSingleton;
/**
* private constructor so that other classes can't
* instantiate it through constructor
* any instantiation while creating new singleton instance should come here
*/
private ThreadSafeSingletonWithDoubleLocking() {}
public static synchronized ThreadSafeSingletonWithDoubleCheckedLocking getInstance() {
if(threadSafeSingleton == null) {
synchronized(ThreadSafeSingletonWithDoubleCheckedLocking.class) {
threadSafeSingleton = new ThreadSafeSingletonWithDoubleCheckedLocking();
}
}
return threadSafeSingleton;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment