When any thread enters a synchronous code block, the synchronization method must have a lock on the synchronization monitor, and when will the lock be released? In a program, the lock on the synchronization monitor cannot be explicitly released, and the lock is released in the following cases.
1, the current thread synchronization method, code block execution end of the time to release
2, the current thread in the synchronization method, synchronization code block encountered a break, return finally the code block or method when the release.
3 、。。。。 An unhandled error or exception causes the exception to be released at the end of the event
4 、。。。。 The program executes the synchronization object Wait method, the current thread is paused, the lock is released
============================================================== does not release the lock if the following conditions are =====================================
1 、。。。。。。 Program calls Thread.Sleep () Thread.yield () These methods suspend execution of the thread and are not freed.
2. When a thread executes a synchronous code block, other threads call the Suspend method to suspend the thread, and the thread does not release the lock, so we should avoid using suspend and resume to control the thread
When is the Java Sync Lock released?