When is the Java Sync Lock released?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.