Java multithreaded Readwritelock

Source: Internet
Author: User

The Readwritelock is a read-write separation lock provided by JDK 1.5, which reduces lock contention. For example, threads A1, A2, and A3 write, Threads B1, B2, and B3 are read, and if a re-entry or internal lock is used, then it is theoretically a serial operation between all reads, between read and write, and between writes. B2 and B3 need to wait while B1 performs a read operation. Because read operations do not compromise the integrity of the data, this wait is unreasonable, and the read-write lock solves the problem. In concurrent situations, read-write locks allow multiple threads to read at the same time, allowing B1, B2, and B3 to be truly parallel. But with data integrity in mind, there is a need to wait between writes and read and write operations.

Access constraints for read-write locks are as follows:

An exclusive lock means that only one thread is allowed to access at the same time, while a read-write lock is not an exclusive lock, allowing multiple read threads to access at the same time, but all read and other write threads are blocked when the write thread accesses. The read-write lock maintains a pair of locks, namely a read lock and a write lock. Usually read-write lock performance is better than exclusive lock, because most scenes read more than write. If read far more than write, then read-write lock function most obvious.

Java and the package provides a read-write lock implementation is Reentrantreadwritelock, the following features:

Readwritelock only defines two methods for acquiring read and write locks, namely Readlock () and Writelock (). In addition to implementing the interface method, Reentrantreadwritelock also provides some methods for external monitoring of the internal working State, and the method name and description are as follows:

  

Resources

"Practical Java High concurrency program design" p85-87

The art of Java concurrent Programming 5.4.1 interface and example of read-write lock

Java multithreaded Readwritelock

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.