Print odd and even numbers using two threads in Java I was asked below question in an interview where I need to print out even and odd numbers using two threads so I came up with below code one without synchronization and other with synchronization.

/**
 * Without synchronization
 *
 */
class Print {
  private static final int MAX = 10;
  private static int count = 1;
  private boolean isOdd = true;

  public void printEven() {
    while (true) {
      if (count &gt; MAX)
        break;
      if (!isOdd) {
        System.err.println(Thread.currentThread().getName() + " : " + count++);
        isOdd = true;
      }
    }
  }

  public void printOdd() {
    while (true) {
      if (count &gt; MAX)
        break;
      if (isOdd) {
        System.err.println(Thread.currentThread().getName() + " : " + count++);
        isOdd = false;
      }
    }
  }
}




/**
 * Using synchronization
 *
 */
class Print2 {
  private static final int MAX = 10;
  private static int count = 1;
  private Object obj = new Object();

  public void printEven() {
    while (true) {
      if (count &gt; MAX)
        break;
      synchronized (obj) {
        System.err.println(Thread.currentThread().getName() + " : " + count++);
        obj.notify();
        try {
          obj.wait();
        } catch (InterruptedException e) {
        }
      }
    }
  }

  public void printOdd() {
    while (true) {
      if (count &gt; MAX)
        break;
      synchronized (obj) {
        System.err.println(Thread.currentThread().getName() + " : " + count++);
        obj.notify();
        try {
          obj.wait();
        } catch (InterruptedException e) {
        }
      }
    }
  }
}


public class PrintEvenOddTester {
  public static void main(String[] args) {
    Print p = new Print();
    Thread t1 = new Thread(() -&gt; p.printEven());
    t1.setName("EVEN");
    Thread t2 = new Thread(() -&gt; p.printOdd());
    t2.setName("ODD");

    t1.start();
    t2.start();
  }
}


I wanted to check whether there is any better or efficient way to do these kind of tasks? I ran both the code and it works fine so wanted to check if everything looks good from thread safety and synchronization perspective.

Since in my above code, I am using while(true) loop which will take some CPU's so I am not sure whether this is the best way to do it?
In Print both variables count and isOdd should be marked as volatile then it would be thread safe, but not very efficient.

In Print2 you assume that no one else could wake up your thread which is incorrect as thread could be awaked by many things - in such light you need to have additional if check there to check if it really should print the variable - like this isOdd in the Print. Having this wait/notify with a volatile isOdd check would be a solution that is both tread safe and efficient by not spinning the loops unnecessarily, count should also be volatile as it is shared beetwen threads.

If you want to learn java concurrency well you can read a book "Java Concurrency in Practice" - it will give you a good understanding of this subject and will allow you to pass inteview questions like this one.

You can ask here or on SO for specific things about java concurrency, but I think that it would be hard and inefficient for you to learn it this way. Much better (faster, less painfull)  approach would be to read this book first and then ask questions if something will stay unclear for you. Good wishes!