...
The idiom can also be extended to protect static state by declaring the lock as private, static and final. If a static method is declared synchronized
, the intrinsic lock of the Class object is obtained, and released when the method completes. Any untrusted code that can access an object of the class, or a subclass, can use the getClass()
method to obtain access to the Class object. Reducing the accessibility of the class to package-private may offer some reprieve when using strategies other than internal locking.
In addition, this This idiom can also be suitably used by classes designed for inheritance. If a superclass thread requests a lock on the object's monitor, a subclass thread can interfere with its operation because of lock reentrancy. This idiom separates the locking strategy of the superclass from that of the subclass. It also permits fine grained locking as opposed to coarse grained because multiple lock objects can then be used for seemingly unrelated operations. This increases overall responsiveness of the application.
...
If a superclass uses an internal private lock to synchronize shared data, subclasses must also use an internal private lock. However, if it uses intrinsic synchronization over the class object without documenting this policy, subclasses may not use intrinsic synchronization over their own class object, unless they explicitly document this locking policy. If the superclass documents this policy by stating that client-side locking is supported, the subclasses have the option of choosing between intrinsic locking over the class object and an internal private lock. Regardless of what is chosen, subclasses must document the locking policy. Refer to the guideline CON10-J. Do not override thread-safe methods with thread-unsafe methods for related information.
If these restrictions are not met, the object's intrinsic lock is not trustworthy. If all conditions are satisfied, then the object gains no significant security from using a private internal lock object, and may synchronize using its own intrinsic lock.
...
Code Block | ||
---|---|---|
| ||
public class SomeObject { private volatile Object lock = new Object(); public void changeValue() { synchronized (lock) { // ... } } public void setLock(IntegerObject lockValue) { lock = lockValue; } } |
Any thread can modify the field's value to refer to some other object in the presence of an accessor such as setLock()
. This might cause two threads that intend to lock on the same object to lock on different objects, enabling them to execute the two critical sections in an unsafe manner. For example, if one thread is in its critical section and the lock is changed, a second thread would lock on the new reference instead of the old one.
Compliant Solution (private final internal lock object)
Thread-safe classes that use the intrinsic synchronization of their respective objects may be protected by using the private lock object idiom and adapting them to use block synchronization. In this compliant solution, if the method changeValue()
is called, the lock is obtained on a private final
Object
instance that is inaccessible from the caller.
...
Using a private final lock may can only be achieved with block synchronization. Block synchronization is sometimes preferred over method synchronization, because operations that do not require synchronization can be moved outside the synchronized region which reduces the overall execution time. There Note that there is no need to declare lock
as volatile because of the strong visibility semantics of final fields. Instead of using setter methods to change the lock, declare and use multiple internal lock objects to achieve the necessary fine-grained locking semantics.
...
The untrusted code attempts to acquire a lock on the class object's monitor and upon succeeding, introduces an indefinite delay which holds up the synchronized
changeValue()
method from acquiring the same lock. This
A complete implementation of this noncompliant code example is would be in compliance with CON32-J. Internally synchronize classes containing accessible mutable static fields. However, the untrusted code violates CON20-J. Do not perform operations that may block while holding a lock.
...
Thread-safe classes that use intrinsic synchronization of over the class object may be protected by using should be refactored to use a static private lock object and block synchronization.
...
In this compliant solution, if the method ChangeValue()
is called, the lock is obtained on a static
private
Object
that is inaccessible from the caller. Using a private lock may only be achieved with block synchronization, as static method synchronization always uses the intrinsic lock of the object's class. Moreover, block synchronization is also preferred over method synchronization, because it is easy to move operations out of the synchronized block when they might take a long time and they are not truly a critical section.
Exceptions
EX1: A class may violate this guideline, if all the following conditions are met:
...