title: Thread won't release lock after wait()
body: <p>Sorry if there are other similar questions but I couldn't find any, I am a beginner to concurrent programming and this problem has been bugging me for awhile, which I really need to understand what is the mistake I have made, or else I couldn't proceed into my assignment.</p>
  
  <p>What I am trying to achieve is to print "1" from 'Test' that is handled by a thread and print "from Test 2" from 'Test 2' which is also handled by a thread, but only "1" is printed. What should I do?</p>
  
  <pre><code>================================
  
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class Test implements Runnable {
  
      Third third;
  
      public Test(Third third){
          this.third = third;
      }
  
      public void run() {
          while (true) {
              synchronized (third) {
                  try {
                      System.out.println("1");
                      third.wait();
                  } catch (InterruptedException ex) {
  
                  }
  
              }
          }
      }
  }
  
  =====================
  
  
  public class Test2 implements Runnable{
      Test test;
      Third third;
  
      public Test2(Test test, Third third){
          this.test = test;
          this.third = third;
      }
  
      public void run(){
          while(true){
              synchronized(third){
                  third.notifyAll();
                  System.out.println("from test 2");
              }
          }
      }
  }
  
  ================================
  
  public class Third {
  
  }
  
  ==============================
  
  public class main {
      public static void main(String[] args) throws InterruptedException{
  
          Third third = new Third();
          Test test = new Test(third);
          Test2 test2 = new Test2(test, third);
  
          Thread t1 = new Thread(test);
          Thread t2= new Thread(test2);
  
          t1.run();
          t2.run();
          t2.join();
          t1.join();
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/RTVcz.png" rel="nofollow noreferrer">The output</a></p>