Last active
August 29, 2015 14:19
-
-
Save yatatsu/71828c1c0ad02add2afe to your computer and use it in GitHub Desktop.
Singleton
This file contains hidden or 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 SingletonClass { | |
private static SingletonClass instance; | |
private SingletonClass() {} | |
// basic style | |
public static SingletonClass getInstance() { | |
if (instance == null) { // <- not thread safe | |
instance = new SingletonClass(); | |
} | |
return instance; | |
} | |
} |
This file contains hidden or 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 SingletonClass { | |
private static volatile SingletonClasss instance; | |
private SingletonClass() {} | |
// double checking lock | |
public static SingletonClass getInstance() { | |
if (instance == null) { | |
synchronized { | |
if (instance == null) { | |
instance == new SingletonClass(); | |
} | |
} | |
} | |
return instance; | |
} | |
} |
This file contains hidden or 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 SingletonClass { | |
private SingletonClass() {} | |
// lazy and thread safe | |
private static class InstanceHolder { | |
private static final SingletonClass instance = new SingletonClass(); | |
} | |
public static SingletonClass getInstance() { | |
return InstanceHolder.instance; | |
} | |
} |
This file contains hidden or 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 SingletonClass { | |
private static final SingletonClass instance; | |
private SingletonClass() {} | |
// when class loaded | |
static { | |
instance = new SingletonClass(); | |
} | |
// thread safe | |
public static SingletonClass getInstance() { | |
return instance; | |
} | |
} |
This file contains hidden or 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 SingletonClass { | |
private static SingletonClass instance; | |
private SingletonClass() {} | |
// it is also thread safe. | |
public static synchronized SingletonClass getInstance() { | |
if (instance == null) { | |
instance = new SingletonClass(); | |
} | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment