The lock--lock interface in Java

Source: Internet
Author: User

After Java SE5, a new lock interface (and related implementation classes) is added to the package to implement the lock function. Although it is less (through the synchronized block or the method provided) to obtain the convenience of the release lock implicitly, but it has the lock acquisition and release of the operational, interruptible acquisition lock and time-out to acquire locks and other synchronized keywords do not have the synchronization characteristics.

The use of the Synchronized keyword implicitly acquires the lock, but it solidifies the acquisition and release of the lock, which is the first fetch and release. Of course, this simplifies the management of synchronization, but extensibility does not show the lock to get and release well.

The key features that are not available with the Synchronized keyword provided by the lock interface are as follows:

Characteristics Describe
Try to get the lock non-blocking The current thread attempts to acquire a lock, and if the lock is not acquired by another thread, the lock is successfully acquired and held
can be interrupted to acquire the lock Unlike the synchronized keyword, a thread that acquires a lock can respond to an interrupt, and when the thread that acquires the lock is interrupted, the interrupt exception is thrown and the lock is freed
Timeout Acquisition lock Locks are acquired before the established deadline, and if the closing time is still unable to acquire the lock, the return

The lock--lock interface in Java

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.