All Downloads are FREE. Search and download functionalities are using the official Maven repository.

resources.report.rules.pmd.NonThreadSafeSingleton.html Maven / Gradle / Ivy



NonThreadSafeSingleton

NonThreadSafeSingleton

Non-thread safe singletons can result in bad state changes. Eliminate static singletons if possible by instantiating the object directly. Static singletons are usually not needed as only a single instance exists anyway. Other possible fixes are to synchronize the entire method or to use an initialize-on-demand holder class.

Refrain from using the double-checked locking pattern. The Java Memory Model doesn’t guarantee it to work unless the variable is declared as volatile, adding an uneeded performance penalty. Reference

See Effective Java, item 48.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.NonThreadSafeSingletonRule

Example(s):

private static Foo foo = null;

//multiple simultaneous callers may see partially initialized objects
public static Foo getFoo() {
    if (foo==null) {
        foo = new Foo();
    }
    return foo;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy