Skip to content

Double-Checked Locking #66

@QiAnXinCodeSafe

Description

@QiAnXinCodeSafe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions