-
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
Getting a singleton calls the initializer() every time #1
Comments
Good catch. Originally was using getOrPut but was concerned about thread safety. Will look into this more. Happy to accept Pull Requests if you have ideas. |
Not perfect but the only hole is a race condition of N threads racing to initialize at same time could call factory more than once, but all will return same instance. Without JDK 8 with internal computeIfAbsent, it is harder without more locking to be surer. I'll see what M13 implementation looks like as well.
|
Ah, the M13 std-lib version is mostly the same logic, just prettier:
|
Ah nice, makes sense. Thanks! |
When you call to get the singleton, the putIfAbsent() calls the initializer every time a get is done. That could cause unintended consequences. especially if the initializer is expensive or has side effect.
The text was updated successfully, but these errors were encountered: