Skip to content
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

double-check singleton has been executed TWICE #183

Open
alalag1 opened this issue May 5, 2022 · 0 comments
Open

double-check singleton has been executed TWICE #183

alalag1 opened this issue May 5, 2022 · 0 comments
Assignees
Milestone

Comments

@alalag1
Copy link
Member

alalag1 commented May 5, 2022

Expected behavior

minimize concurrent operations

Actual behavior

double-check singleton has been executed TWICE

Object getInstanceThenInit() {
Object instance = getInstance();
if (!initialized) {
synchronized (this) {
if (!initialized) {
context.handlerFactory().orElseThrow(() ->
new IllegalStateException("HandlerFactory is not present while initializing object," +
" clazz: " + clazz))
.doInit(instance, null);
initialized = true;
}
}
}
return instance;
}
Object getInstance() {
if (target != null) {
return target;
}
synchronized (this) {
if (target == null) {
target = context.handlerFactory().orElseThrow(() ->
new IllegalStateException("HandlerFactory is not present while instantiating class: " + clazz))
.instantiate(clazz, null);
}
}
return target;
}
}

Steps to reproduce

Env

  • Restlight version: 1.0.0-SNAPSHOT
@LCDZhao-Z LCDZhao-Z assigned LCDZhao-Z and unassigned LCDZhao-Z May 6, 2022
@LCDZhao-Z LCDZhao-Z added this to the v1.0.1 milestone May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants