Two threads executing synchronized block simultaneously Two threads executing synchronized block simultaneously multithreading multithreading

Two threads executing synchronized block simultaneously


The answer lies in java.lang.Object.wait(long) whose documentation says:

[...] This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. [...]


Use

Thread.sleep(5000);

JavaDocs for Thread.sleep:

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.


The following quote from the Oracle Tutorials explains the situation:

When wait is invoked, the thread releases the lock and suspends execution.

Also, only one thread can execute a synchronized block guarded by the same object! Calling wait in your example releases the lock, thus allowing another thread to acquire the lock.