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