title: How to switch between two thread back and forth
body: <p>I have two methods in two different classes, like this</p>
  
  <pre><code>public class ClassX implements Runnable {
  
      public  void methodAandB() {
          for(int i=0;i&lt;10;i++) {
              System.out.println("This is A and B ");
          }
      }
      @Override
      public void run() {
          methodAandB();
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class ClassY implements Runnable {
  
      public void methodAorB() {
          for(int i=0;i&lt;10;i++) {
              System.out.println("This is A or B");
          }
      }
  
      @Override
      public void run() {
          methodAorB(a);
      }
  }
  </code></pre>
  
  <p>Thread <em>t1</em> is calling <code>methodAandB()</code>.</p>
  
  <p>Thread <em>t2</em> is calling <code>methodAorB()</code>.</p>
  
  <hr>
  
  <p>Can I switch between these two threads after each iteration of loop in methods?</p>
  
  <p>I want to get output like this:</p>
  
  <blockquote>
    <p>This is A and B</p>
    
    <p>This is A or B</p>
    
    <p>This is A and B</p>
    
    <p>This is A or B</p>
    
    <p>This is A and B</p>
    
    <p>This is A or B</p>
    
    <p>This is A and B</p>
    
    <p>This is A or B</p>
  </blockquote>