Read-write lock notes

Read-write lock notes

Overview

Read-write locks are divided into read locks and write locks.
- Read locks can coexist without write locks
- Write locks can only exist one at a time, and while read locks exist, read locks cannot exist

ReentrantReadWriteLock

Analysis of the principle of read-write lock through ReentrantReadWriteLock

initialization

private ReadWriteLock lock = new ReentrantReadWriteLock();
private Lock readLock = lock.readLock();
private Lock writeLock = lock.writeLock();

Like ReentrantLock, ReentrantReadWriteLock also uses AQS as a synchronizer, and also provides two forms of fairness and unfairness. WriteLock uses AQS acquireand releasemethods, and ReadLock uses AQS acquireSharedand releaseSharedmethods.
Further explanation is given next

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895439&siteId=291194637