Printing a sequence of numbers using two alternating threads
I'm not sure what you expect from writing an if-else construct where both the if and the else block contain exactly the same code. Your run() method is effectively equivalent to the following:

public synchronized void run() {
    while (i &lt; 10) {
        try {
            notify();
            System.out.println(Thread.currentThread().getName() + " prints " + i);
            i++;
            wait();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Other than that, your usage of notify() and wait() looks fine. However, there is a phenomenon called a "spurious wakeup", which means that a thread can be awakened for no apparent reason at all. This is very unlikely, but theoretically, it is possible that one of your two threads is spuriously woken up after it enters the waiting state but before the other thread acquires the lock on obj, which could result in one thread printing two consecutive numbers if the thread that was spuriously woken up re-acquires the lock. You might take a look at this.

Also, your program never terminates. You can rectify this by calling notify() or notifyAll() after the while loop.