Home > Java > javaTutorial > Behavioral analysis of queue jumping under different lock modes in Java.

Behavioral analysis of queue jumping under different lock modes in Java.

PHPz
Release: 2023-04-23 09:13:17
forward
1214 people have browsed it

1. ReentrantReadWriteLock can set fair lock mode and unfair lock mode.

// 公平锁模式
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);
//非公平锁模式 默认情况
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(false);
Copy after login

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();
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template