Both threads are synchronized on the same Object here, namely b . main() first uses the lock, but then calls b.wait() , which releases the lock and waits for someone to call notify() on b .
This means that when the run() method, which in this case is called on b , calls notify() , this will again awaken the main() method.
Thus, locking on b is not very important here, the important part is that both threads have the same lock, or the wait()/notify() collaboration will not work.
source share