title: synchronized thread a simple produce reduce problem I can not understand. why Thread.sleep inside synchronized block makes it work different?
body: <p>I&quot;m trying to learn a simple reduce produce problem. I have a limit of 20 items and I need to stop produce when I have too many of them. I found a way to make it work but now I cannot understand why it works that way.
  This is the first version That is not working so well. the Thread.sleep is inside the synchronized block. and it seems that it is not working so well. it is producing like 15-16 items and only then sells 15-20 of them and then again producing alot.</p>
  <pre><code>public class Factory {
       int items = 0;
  
  
  
      public void produce() throws InterruptedException {
             while(true){
                 synchronized (this){
                     while(items&gt;=20)
                         this.wait();
                     items++;
                     System.out.println(items+&quot; produced&quot;);
                     this.notify();
                     Thread.sleep(1000);
                 }
  
             }
      }
  
      public void sell() throws InterruptedException {
          while(true){
              synchronized (this){
                  while(items==0) {
                      this.wait();
                  }
                  items--;
                  System.out.println(items+&quot; sold&quot;);
                  this.notify();
                  Thread.sleep(1000);
  
              }
              
  
          }
      }
  
  
  
  
  }
  </code></pre>
  <p>now when I take the sleep out of the synchronized block to the while block everything works great and I just cannot understand why.</p>
  <pre><code>public class Factory {
       int items = 0;
       Object lock = new Object();
  
  
  
      public void produce() throws InterruptedException {
             while(true){
                 synchronized (this){
                     while(items&gt;=20)
                         this.wait();
                     items++;
                     System.out.println(items+&quot; produced&quot;);
                     this.notify();
                 }
                 Thread.sleep(1000);  // THIS LINE CHANCGED PLACE
             }
      }
  
      public void sell() throws InterruptedException {
          while(true){
              synchronized (this){
                  while(items==0) {
                      this.wait();
                  }
                  items--;
                  System.out.println(items+&quot; sold&quot;);
                  this.notify();
  
              }
              Thread.sleep(1000);  // THIS LINE CHANCGED PLACE
  
          }
      }
  
  
  
  
  }
  </code></pre>
  <p>Thanks for the help!</p>
  <p>Main:</p>
  <pre><code>public class Main {
      public static void main(String[] args) {
          Factory myFactory = new Factory();
          Thread produce = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      myFactory.produce();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }); produce.start();
  
          Thread sell = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      myFactory.sell();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }); sell.start();
  
          try {
              produce.join();
              sell.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>