Created
October 1, 2012 09:51
-
-
Save AlexanderBrevigSublime/3810624 to your computer and use it in GitHub Desktop.
Java: Thread safe singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YourSingletonClass { | |
public static synchronized YourSingletonClass getInstance() { | |
if (instance == null) { | |
instance = new YourSingletonClass(); | |
} | |
return instance; | |
} | |
public Object clone() throws CloneNotSupportedException { | |
throw new CloneNotSupportedException(); | |
} | |
private YourSingletonClass() { | |
// construct your class | |
} | |
private static YourSingletonClass instance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment