title: Execute two threads which wait one for the other while main thread continues
body: <p>How can I start two threads where thread1 executes first, thread2 starts when thread1 ends while the main method thread can continue its work without locking on the other two?</p>
  
  <p>I have tried join() however it needs to be called from the thread which has to wait for the other, there's no way to do something like thread2.join(thread1);
  If I call for a join inside main() I therefore effectively stop execution of the main thread and not only of thread2.</p>
  
  <p>I therefore tried with ExecutorService but again same problem.</p>
  
  <pre><code>import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.TimeUnit;
  
  public class Test
  {
      public static void main(String args[]) throws InterruptedException
      {
          System.out.println(Thread.currentThread().getName() + " is Started");
  
          class TestThread extends Thread
          {
              String name;
              public TestThread(String name)
              {
                  this.name = name;
              }
  
              @Override
              public void run()
              {
                  try
                  {
                      System.out.println(this + " is Started");
                      Thread.sleep(2000);
                      System.out.println(this + " is Completed");
                  }
                  catch (InterruptedException ex)  {  ex.printStackTrace(); }
              }
  
              @Override
              public String toString()  { return "Thread " + name; }
          }
  
          ExecutorService executor = Executors.newCachedThreadPool();
          executor.execute(new TestThread("1"));
  
          boolean finished = executor.awaitTermination(1, TimeUnit.HOURS);
  
          if(finished)
          {
             //I should execute thread 2 only after thread 1 has finished
              executor.execute(new TestThread("2"));
          }
  
          //I should arrive here while process 1 and 2 go on with their execution
          System.out.println("Hello");
      }
  }
  </code></pre>
  
  <p><strong>#EDIT: Why I need this:</strong></p>
  
  <p>I need this because Thread1 copies elements from a database table into another database, thread2 has to copy a linking table which references the table copied from thread1.
  Consequently thread2 has to start populating its linking table only when thread1 has finished otherwise an integrity error is given by the database.
  Now imagine I have several threads with different priorities due to complex linking tables and you have an idea.</p>