title: Waiting for condition in Java
body: <p>I want to make one thread that puts values to a queue when it gets empty and wait for this condition while it is not. Here's the code I've tried to use, but it prints</p>
  
  <pre><code>Adding new
  Taking Value 1
  Taking Value 2
  Taking Value 3
  Taking Value 4
  </code></pre>
  
  <p>So it is working only one time. What is the problem?</p>
  
  <pre><code>import java.util.concurrent.BlockingQueue;
  import java.util.concurrent.LinkedBlockingQueue;
  
  
  public class SO {
      public String test;
      public String[] list = new String[] {test};
  
      public static void main(String[] args) {
          new SO();
      }
  
      public SO() {
          go();
      }
  
      BlockingQueue&lt;String&gt; qq = new LinkedBlockingQueue&lt;String&gt;();
  
      class Producer implements Runnable {
          public void run() {
              try {
                  while (true) {
                      synchronized (this) {
                          while (qq.size() &gt; 0)
                              wait();
  
                          System.out.println("Adding new");
                          qq.put("Value 1");
                          qq.put("Value 2");
                          qq.put("Value 3");
                          qq.put("Value 4");
                      }
                  }
              } catch (InterruptedException ex) {}
          }
      }
  
      class Consumer implements Runnable {
          public void run() {
              try {
                  while(true) {
                      System.out.println("Taking " + qq.take()+". "+String.valueOf(qq.size())+" left");
                      Thread.sleep(1000);
                  }
              } catch (InterruptedException ex) {}
          }
      }
  
      public void go() {
          Producer p = new Producer();
          Consumer c = new Consumer();
  
          new Thread(p).start();
          new Thread(c).start();
      }
  }
  </code></pre>