Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixing code sample

...

Only the run() method from the noncompliant code example is modified, as follows:

Code Block
bgColor#ccccff
public class ProcessStep implements Runnable {
  private static final Lock lock = new ReentrantLock();
  private static final Condition condition = lock.newCondition();
  private static int time = 0;
  private final int step; // Do operations when field time reaches this value
  public ProcessStep(int step) {
    this.step = step;
  }


    @Override public void run() {
      lock.lock();
      try {
        while (time != step) {
          condition.await();
        }
  
        // Perform operations

        time++;
        condition.signalAll();
      } catch (InterruptedException ie) {
        Thread.currentThread().interrupt(); // Reset interrupted status
      } finally {
        lock.unlock();
      }
    }

}

Compliant Solution (Unique Condition Per Thread)

...