title: Two threads, two synchronized blocks and one intrinsic lock
body: <p>I wrote a code snippet that starts two threads; one thread prints all odd numbers while another thread prints all even numbers.
  I used a combination of intrinsic lock and thread communication commands to achieve proper interleaving of my two threads.
  Here is my code,</p>
  
  <pre><code>public class threadEvenOdd implements Runnable
  {
      static Boolean isOdd=true;
      int count = 10;
      Boolean value;
      static int c=1;
      static Object lock = new Object();
  
      threadEvenOdd(Boolean temp)
      {
          value = temp;
      }
      public void run()
      {
          if(value)
          {
              printOdd(count);
          }
          if(!value)
          {
              printEven(count);
          }
      }
      void printOdd(int count)
      {
          try
          {
              for(int i=0;i&lt;count/2;i++)
              {
                  //System.out.println("odd enters lock");
                  synchronized(lock)
                  {
                      if(!isOdd)
                      {
                          //System.out.println("odd in barrier");
                          lock.wait();
                      }
                      System.out.println(c);
                      c++;
                      isOdd = false;
                      //System.out.println("odd notifies");
                      lock.notify();
                  }
              }
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
      }
      void printEven(int count)
      {
          try
          {
              for(int i=0;i&lt;count/2;i++)
              {
                  //System.out.println("even enters lock");
                  synchronized(lock)
                  {
                      if(isOdd)
                      {
                          //System.out.println("even in barrier");
                          lock.wait();
                      }
                      System.out.println(c);
                      c++;
                      isOdd = true;
                      //System.out.println("even notifies");
                      lock.notify();
                  }
              }
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
      }
      public static void main (String args[])
      {
          threadEvenOdd th1 = new threadEvenOdd(true);
          threadEvenOdd th2 = new threadEvenOdd(false);
          Thread t1 = new Thread(th1);
          t1.setName("odd");
          Thread t2 = new Thread(th2);
          t2.setName("even");
          //System.out.println(t1.getName() + " starts");
          t1.start();
          //System.out.println(t2.getName() + " starts");
          t2.start();
      }
  }
  </code></pre>
  
  <p>Here are my questions:</p>
  
  <ol>
  <li><p>The odd thread executes in the printOdd() function, while the even thread executes in the printEven() function. I am using one intrinsic lock for both threads; I don't understand how the two threads can exist in their respective synchronized blocks at the same time, because the use the same lock.</p></li>
  <li><p>I removed the thread communication statements(notify, wait) from my code and still I obtained my desired output. I am now wondering if my code actually needs the thread communication statements at all.</p></li>
  <li><p>I guess I still need to work on my understanding of multithreading concepts, as I am struggling to understand my own code :p Can anyone explain if there is a better way to do this using only the multithreading concepts that I have used?</p></li>
  </ol>