What conditions in Java cause object locks to be freed

Source: Internet
Author: User
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

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.