Skip to content

Files

Latest commit

 

History

History
26 lines (18 loc) · 858 Bytes

NestedSynchronization.md

File metadata and controls

26 lines (18 loc) · 858 Bytes

Pattern: Nested synchronization

Issue: -

Description

Nested synchronized statements are either useless (if the lock objects are identical) or prone to deadlock.

Note that a closure or an anonymous inner class carries its own context (scope). A synchronized statement within a closure or an anonymous inner class defined within an outer synchronized statement does not cause a violation (though nested synchronized statements within either of those will).

Here is an example of code that produces a violation:

def someMethod() {
    synchronized(this) {
        // do something ...
        synchronized(this) {
            // do something else ...
        }
    }
}

Further Reading