Java multi-threaded lock release issue

Java multi-threaded operating environment, will cause the object lock is released and under what circumstances?

A: Because the thread waits for a lock only after obtaining the lock, to resume operation, so let thread holding the lock promptly release the lock when the lock is no longer needed is very important. In the following cases, the thread holding the lock releases the lock:
(1) complete the implementation of synchronized block, it will release the lock. (The synchronized)
(2) during the execution of the synchronization code block, resulting in the thread terminates exception is encountered, the lock will be released. (Exception)
(. 3) during the execution of the synchronization code block, performing a lock belongs object wait () method, this thread releases the lock, enter
        into the waiting pool object. (wait)

in addition to the above, as long as the thread holding the lock does not end synchronization code block is executed, it will not release the lock.
In the following cases, the thread of the lock is not released:
during execution of the synchronization code block (1) is performed to Thread.sleep (), the current thread to give the CPU, sleep starts, enters blocked state, not in sleep release the lock.
(2) during the execution of the synchronization code block, do the Thread.yield () method, the current thread to give up CPU, return to the Ready state, but will not release the lock.

Guess you like

Origin www.cnblogs.com/fyscn/p/11364065.html