Created
November 4, 2013 12:53
-
-
Save kyze8439690/7302016 to your computer and use it in GitHub Desktop.
singleton pattern
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
public class Singleton { | |
private static Object obj = new Object(); | |
private static Singleton instance = null; | |
private Singleton(){ | |
} | |
public static Singleton getInstance() { | |
// if already inited, no need to get lock everytime | |
if (instance == null) { | |
synchronized (obj) { | |
if (instance == null) { | |
instance = new Singleton(); | |
} | |
} | |
} | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment