Java_ Multi-threaded _ Lock Release Q: In the context of a Java multithreaded runtime, in which cases will the object lock be freed?
A: Since the thread waiting for a lock can only be resumed after the lock is acquired, it is important that the thread holding the lock release the lock in time when the lock is no longer needed. The thread holding the lock releases the lock in the following cases:
(1) When the synchronization code block is executed, the lock is released. (synchronized)
(2) During the execution of a synchronous code block, an exception is encountered, causing the thread to terminate, and the lock is freed. (Exception)
(3) During the execution of a synchronous code block, the Wait () method of the object to which the lock belongs is executed, which releases the lock, enters the
The wait pool into the object. (wait)
In addition to the above, locks are not released as long as the thread holding the lock has not finished executing the synchronization code block.
In the following scenario, the thread does not release the lock:
(1) During the execution of the synchronization code block, the Thread.Sleep () method is executed, the current thread abandons the CPU, begins to sleep, and does not release the lock during sleep.
(2) During the execution of the synchronization code block, the Thread.yield () method is executed, and the current thread discards the CPU but does not release the lock.
(3) During the execution of a synchronous code block, other threads execute the Suspend () method of the current thread object, and the current thread is paused, but the lock is not released. Transferred from: http://blog.sina.com.cn/s/blog_51335a000101nxlb.html
What conditions in Java cause object locks to be freed