Skip to content

Files

Latest commit

 

History

History
75 lines (56 loc) · 1.64 KB

SynchronizedOnString.md

File metadata and controls

75 lines (56 loc) · 1.64 KB

Pattern: Synchronized on string

Issue: -

Description

Synchronization on a String field can lead to deadlock. Constant strings are interned and shared across all other classes loaded by the JVM. Thus, this could is locking on something that other code might also be locking. This could result in very strange and hard to diagnose blocking and deadlock behavior.

Examples:

class SomeClass {

    final String stringLock = "stringLock"

    def method() {
        // violation
        synchronized(stringLock) { }
    }
}

class SomeClass {

    final String stringLock = "stringLock"

    class SomeInnerClass {
        def method() {
            synchronized(stringLock) { }
        }
    }
}

class SomeClass {
    // implicit typing
    final def stringLock = "stringLock"

    def method() {
        // violation
        synchronized(stringLock) { }
    }
}

class SomeClass {
    // implicit typing
    final def lock = new Object[0] // correct idiom

    def method() {
        return new Runnable() {
            final def lock = "" // shadows parent from inner class
            public void run() {
                // violation
                synchronized(stringLock) { }
            }
        }
    }
}

class SomeClass {
    // implicit typing
    final def lock = new Object[0] // correct idiom

    class SomeInnerClass {

        final def lock = "" // shadows parent from inner class
        def method() {
            // violation
            synchronized(stringLock) { }
        }
    }
}

Further Reading