Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: ok, i'm done; please review

Concurrency related issues manifest themselves when assumptions are made about the multithreaded behavior of derived classesOverriding thread-safe methods with methods that are not thread-safe can result in improper synchronization if the client inadvertently operates on an instance of the thread-unsafe subclass. An overridden synchronized method's contract may be violated if a subclass provides an implementation that is not safe for concurrent use. .

Overriding thread-safe methods with methods that are not thread-safe is not in itself an error, but is disallowed by this guideline because it may easily lead to errors that are otherwise difficult to diagnose.

The locking strategy of classes designed for inheritance should always be documented. This information can subsequently be used to determine an appropriate locking strategy of subclasses (see The guideline CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code recommends documenting the locking strategy of classes designed for inheritance. This information is useful for deciding the locking strategy of subclasses).

Noncompliant Code Example (synchronized method)

This noncompliant code example defines a overrides the synchronized doSomething() method in class Base with an unsynchronized method in the derived class Derived.

Code Block
bgColor#FFCCCC
class Base {
  public synchronized void doSomething() {
    // ...	
  }
}

class Derived extends Base {
  public void doSomething() {
    // ...		
  }
}

The doSomething() method of class Base can be safely used by multiple threads. However, if a instances of the subclass Derived overrides the method but leaves it unsynchronized, its instance cannot be safely used by multiple threads.

This problem is hard to notice error can be difficult to diagnose because threads that accept instances of Base can also accept instances of its subclasses. Consequently, the threads may incorrectly assume clients may be unaware that the subclasses are are operating on a thread unsafe subclass of a thread-safe class.

Compliant Solution (synchronized method)

...

This compliant solution does not violate CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code because the accessibility of the class is package-private which is allowable when untrusted code cannot infiltrate the package.

Compliant Solution (synchronized method)

This compliant solution ensures that the Derived object is thread-save by overriding the synchronized doSomething() method of the Base class with a method that contains syncrhonizes on a private final lock objects.

Code Block
bgColor#ccccff

class Base {

  public synchronized void doSomething() {
    // ...	
  }
}

class Derived extends Base {

  private final Object lock = new Object();

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

This is an acceptable solution, provided that the Derived class provides a consistent locking policy.

Noncompliant Code Example (private lock)

...