title: how to pause a thread and resume it exactly where it was left off
body: <p>I'm trying to come with a solution for a thread to pause and resume exactly where it was left off. </p>
  
  <p>So here's a sample code emulating my problem: 2 threads are running in the background: <code>taskThread</code> &amp;  <code>busyThread</code>. When <code>busyThread</code> is in <strong>system is busy area</strong>, <code>taskThread</code> must alt/pause immediately and resume exactly where it was left off. Example, if <code>taskThread</code> was paused at task C (finished) it should resume at D.</p>
  
  <p>I tried to use wait, notify on taskThread but without success.  </p>
  
  <pre><code>public class Test
  { 
     private Thread taskThread;
     private Thread busyThread;
  
  public static void main(String args[]) throws Exception
  { 
      Test t = new Test();
      t.runTaskThread();
      t.runBusyThread(); 
  } 
  
  
  public void runTaskThread()
  {
      taskThread = new Thread(new Runnable(){
  
          @Override
          public void run()
          {
              for (int x=0; x&lt;100; x++)
              {
                  try
                  {                       
                      System.out.println("I'm doing task A for process #"+x);
                      Thread.sleep(1000);
  
                      System.out.println("I'm doing task B for process #"+x);
                      Thread.sleep(200);
  
                      System.out.println("I'm doing task C for process #"+x);
                      Thread.sleep(300);
  
                      System.out.println("I'm doing task D for process #"+x);
                      Thread.sleep(800);
  
                      System.out.println("\n\n");
  
                  } catch (InterruptedException e)
                  {                       
                      e.printStackTrace();
                  }                                       
              }
  
          }});
  
      taskThread.start();
  }
  
  public void runBusyThread()
  {
      busyThread = new Thread(new Runnable(){
  
          @Override
          public void run()
          {
              while (true)
              {
                  Random rand = new Random();
                  int randomNum = rand.nextInt(1000);
                  if (randomNum&lt;400)
                  {
                      System.out.println("Wait...system is busy!!!");                     
                      try
                      {       //what should come here to to signal taskThread to paused
                              Thread.sleep(3000);
  
                               //what should come here to to signal taskThread to resume
  
                      } catch (InterruptedException e)
                      { 
                      }
                  } else
                  {
                      try
                      {
                          Thread.sleep(300);
                      } catch (InterruptedException e)
                      {
                      }   
                  }                       
              }               
          }});    
  
      busyThread.start();
  }
  }
  </code></pre>