1. ReentrantReadWriteLock can set fair lock mode and unfair lock mode.
// 公平锁模式 ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true); //非公平锁模式 默认情况 ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(false);
Before obtaining a fair lock, check the readerShouldBlock() method. Before obtaining a write lock, check the writerShouldBlock() method first, and then decide whether to queue or jump in the queue.
2. In unfair lock mode, writerShouldBlock() and readerShouldBlock() implement
final boolean writerShouldBlock() { return false; // writers can always barge } final boolean readerShouldBlock() { return apparentlyFirstQueuedIsExclusive(); }
Unfair lock can jump the queue when acquiring the write lock. Use policy decisions when reading locks.
The above is the detailed content of Behavioral analysis of queue jumping under different lock modes in Java.. For more information, please follow other related articles on the PHP Chinese website!