title: How to use internal flag for wait loop if thread doesn't change state?
body: <p>For testing my understanding wait notify I wrote small program which writes <code>left</code> <code>right</code> in a particular order </p>
  
  <pre><code>public class LeftRightWaitNotifyExample {
      final static String str = "1";
  
      public static void main(String[] args) throws InterruptedException {
  
          new LeftLegThread(str).start();
          Thread.sleep(100);
          new RightLegThread(str).start();
      }
  }
  
  class LeftLegThread extends Thread {
      String monitor;
  
      public LeftLegThread(String str) {
          monitor = str;
      }
  
      @Override
      public void run() {
          try {
              makeStep();
          } catch (InterruptedException e) {          
              e.printStackTrace();
          }
      }
  
      private void makeStep() throws InterruptedException {
          synchronized (monitor) {
              int i=0;
              while (i++ &lt; 10) {
                  System.out.println("Left ");
                  monitor.notify();
                  monitor.wait();
              }
          }
      }
  }
  
  class RightLegThread extends Thread {
      String monitor;
  
      public RightLegThread(String str) {
          monitor = str;
      }
  
      @Override
      public void run() {
          try {
              makeStep();
          } catch (InterruptedException e) {
  
          }
      }
  
      private void makeStep() throws InterruptedException {
          synchronized (monitor) {
              while (true) {
                  System.out.println("Right ");
                  monitor.notify();
                  monitor.wait();
              }
          }
      }
  }
  </code></pre>
  
  <p>But I know that it is bad write following code:</p>
  
  <pre><code> monitor.wait();
  </code></pre>
  
  <p>I should wrap it by loop.</p>
  
  <p>But I cannot imagine loop condition because state doesn't change.</p>
  
  <p>Please, advice how to solve this problem.</p>