yield does not accept or release locks, it just pauses the current thread. Thus, the assignment in the synchronized block will not allow the current thread to block the lock and allows other methods to enter the synchronized block. To release the lock, use the wait/notify .
From Java Language Specification
Thread.sleep causes the current executable thread to sleep (temporarily stop execution) for the specified duration, provided that the system timers and schedulers are accurate and accurate. The thread does not lose ownership of any observers and the resumption of execution will depend on the planning and availability of processors on which to execute the thread.
It is important to note that neither Thread.sleep nor Thread.yield have synchronization semantics . In particular, the compiler does not need to flush records cached into registers into shared memory before calling Thread.sleep or Thread.yield, and the compiler also needs to reload values cached in registers after calling Thread.sleep or Thread.yield.
source share