Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Double-Checked Locking #66

Open
QiAnXinCodeSafe opened this issue Jun 8, 2020 · 0 comments
Open

Double-Checked Locking #66

QiAnXinCodeSafe opened this issue Jun 8, 2020 · 0 comments

Comments

@QiAnXinCodeSafe
Copy link

if (metadata == null) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(clazz);
if (metadata == null) {
LinkedList<InjectionMetadata.InjectedElement> elements;
elements = new LinkedList<InjectionMetadata.InjectedElement>();
parseFields(clazz, annotion, elements);
parseMethods(clazz, annotion, elements);
metadata = new InjectionMetadata(clazz, elements);
this.injectionMetadataCache.put(clazz, metadata);
}

Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment.
Unfortunately, it will not work reliably in a platform independent way when implemented in Java, without additional synchronization.
Declare a volatile local variable metadata offers a much more elegant solution

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant