Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: edits

The synchronized keyword is used to acquire a mutual-exclusion lock so that no other thread can acquire the lock, while it is being held by the executing thread. Recall that there are two ways to synchronize access to shared mutable variables, method synchronization and block synchronization.

The synchronized keyword always uses the object's intrinsic 'monitor' lock. This lock is available to any code that the object itself is available to; consequently any code can lock on the object, and potentially cause a denial of service .Recall that there are two ways to synchronize access to shared mutable variables, method synchronization and block synchronization. (DoS). Excessive synchronization can induce a denial of service ( DoS ) vulnerability because another class whose member locks on the object, can fail to release the lock promptly. However, this requires the victim class to be accessible from the hostile class.

Wiki Markup
The _private lock object_ idiom can be used to prevent thethis DoS vulnerability. The idiom consists of a {{private}} object declared as an instance field. The {{private}} object must be explicitly used for locking purposes in {{synchronized}} blocks, within the class's methods. This lock object belongs to an instance of the object and is not associated with the class itself. Consequently, there is no lock contention between a class method and a method of a hostile class when both try to lock on the object. \[[Bloch 01|AA. Java References#Bloch 01]\]  

...

  • Subclass the class (However, trusted code may subclass the class.)
  • Create an object of the class (or a subclass)
  • Access an object of the class (or a subclass)
    Furthermore, the class must also ensure that no superclasses that it inherits from use any synchronization at all.

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.

Likewise, if a static method has the synchronized keyword, the intrinsic lock of the Class object is obtained, and released when the method completes. The same restrictions listed above apply to static methods , since because 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. Furthermore, hostile code must not be able to access the Class object at all. This could may be accomplished, for instance, by making the class package-private.This guideline is an extension of CON02-J. Always synchronize on the appropriate object and is fully compliant with it. It recommends block synchronization using an internal private lock object instead of synchronizing on the this reference of the class object.

Noncompliant Code Example (method synchronization)

...

The untrusted code attempts to acquire a lock on the object's monitor and upon succeeding, introduces an indefinite delay which holds up the synchronized changeValue() method from acquiring the same lock. Note that the untrusted code also violates CON20-J. Do not perform operations that may block while holding a lock.

Noncompliant Code Example (public

...

non-final lock object)

This noncompliant code example locks on a public nonfinal object non-final object in an attempt to use a lock different from SomeObject's own intrinsic lock.

Code Block
bgColor#FFcccc
public class SomeObject {
  public Object lock = new Object();

  public void changeValue() {
    synchronized (lock) {
      // ...
    }
  }
}

It However, it is possible for untrusted code to change the value of the lock object and foil all attempts to synchronize.

...

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.

...

Thread-safe classes that use the intrinsic synchronization of the object 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.

Code Block
bgColor#ccccff
public class SomeObject {
  private final Object lock = new Object(); // private lock object

  public void changeValue() {
    synchronized (lock) { // Locks on the private Object
      // ...
    }
  }
}

Using a private final lock may 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.

...

Thread-safe classes that use intrinsic synchronization of the class object may be protected by using a static private lock object andblock and block synchronization.

Code Block
bgColor#ccccff
public class SomeObject {
  private static final Object lock = new Object(); // private lock object

  public static void ChangeValue() {
    synchronized (lock) { // Locks on the private Object
      // ...
    }
  }
}

...