Skip to content

Files

Latest commit

 

History

History
46 lines (37 loc) · 1.25 KB

DoubleCheckedLocking.md

File metadata and controls

46 lines (37 loc) · 1.25 KB

Pattern: Double checked locking

Issue: -

Description

This rule detects double checked locking, where a lock hint is tested for null before initializing an object within a synchronized block. Double checked locking does not guarantee correctness and is an anti-pattern.

Example of violations:

if (object == null) {
    synchronized(this) {
        if (object == null) {
            // createObject() could be called twice depending
            // on the Thread Scheduler.
            object = createObject()
        }
    }
}

// there are several idioms to fix this problem.
def result = object;
if (result == null) {
    synchronized(this) {
        result = object;
        if (result == null)
            object = result = createObject()
    }
}

// and a better solution for a singleton:
class someClass  {
    private static class ObjectHolder {
       public static Object object = createObject()
    }
    public static Object getObject() {
        return ObjectHolder.object;
    }
}

Further Reading