Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example violates the liveness property. Each thread has a different condition predicate , as because each requires step to have a different value before proceeding. The Object.notify() method wakes up only one thread at a time. Unless it happens to wake up the thread that is required to perform the next step, the program will deadlock.

...

Code Block
bgColor#ccccff
@Override public void run() {
  try {
    synchronized (lock) {
      while (time != step) {
        lock.wait();
      }

      // Perform operations

      time++;
      lock.notifyAll(); // Use notifyAll() instead of notify()
    }
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt(); // Reset interrupted status
  }
}

Noncompliant Code Example (Condition

...

Interface)

This noncompliant code example is similar to the noncompliant code example for notify() but uses the Condition interface for waiting and notification.

...