-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java Design Pattern: Singleton Pattern #124
Comments
FredWe
changed the title
Java Design Pattern: Singleton Pattern
Java Design Pattern: Singleton Pattern Implementation
Feb 28, 2017
FredWe
changed the title
Java Design Pattern: Singleton Pattern Implementation
Java Design Pattern: Singleton Pattern
Feb 28, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features (wiki):
(Almost) Best Practices:
1. private static internal holder:
The intuitive implementation has thread safety problems (wrong examples, see ref.) This implementation using JVM mechanism itself guarantee the thread safety issue:
SingletonHolder
is private, there is no access exceptgetInstance()
, therefore it's lazy initialization;2. enum typed class
A real elegant implementation. Access from
EasySingleton.INSTANCE
, easier than callinggetInstance()
as above.Creating a enum instance is thread-safe by default, so no thread safety issue. But thread safety of any other method is responsible to programmer himself.
Ref.: http://coolshell.cn/articles/265.html
The text was updated successfully, but these errors were encountered: