Skip to content

Instantly share code, notes, and snippets.

@surajdubey
Created April 14, 2018 04:18
Show Gist options
  • Select an option

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

Select an option

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