Java readandwrite_Java Concurrent Programming ReadWriteLock Read-Write Lock Operation Method_He Mingke’s Blog
1. Introduction to ReadWriteLock Why do we have to use ReadWriteLock when we have Lock? After we lock the shared resource, all threads will wait. Lock read operations are also locked, and write operations are also locked. When reading shared resources, there is actually no need to lock them. Of course, there will be cases where reading and writing exist at the same time. For example, common operations in our database include adding, deleting, modifying, and checking. Adding, deleting, and modifying are all write operations. Write operations must be locked, while read operations can be shared. Not all operations require locking. In order to further improve the reusability and granularity, write operations are exclusive, read operations are shared, and no locks are added. ReadWriteLock manages a set of locks, one is a read-only lock and the other is a write lock. Read locks can be held by multiple threads at the same time without write locks, and write locks are exclusive. That is, reading and reading can be shared, and writing, reading and writing should be exclusive Read operations do not need to care about resource contention and data consistency operations There is no problem with multiple threads reading a…