java - 关于 ReentrantReadWriteLock 的文档矛盾。在公平模式下,最终写锁是否优先于读锁?

标签 java concurrency locking readwritelock

来自 ReentrantLock文档:

Fair mode
When constructed as fair, threads contend for entry using an approximately arrival-order policy. When the currently held lock is released either the longest-waiting single writer thread will be assigned the write lock, or if there is a group of reader threads waiting longer than all waiting writer threads, that group will be assigned the read lock.

A thread that tries to acquire a fair read lock (non-reentrantly) will block if either the write lock is held, or there is a waiting writer thread. The thread will not acquire the read lock until after the oldest currently waiting writer thread has acquired and released the write lock. Of course, if a waiting writer abandons its wait, leaving one or more reader threads as the longest waiters in the queue with the write lock free, then those readers will be assigned the read lock.

A thread that tries to acquire a fair write lock (non-reentrantly) will block unless both the read lock and write lock are free (which implies there are no waiting threads). (Note that the non-blocking ReentrantReadWriteLock.ReadLock.tryLock() and ReentrantReadWriteLock.WriteLock.tryLock() methods do not honor this fair setting and will acquire the lock if it is possible, regardless of waiting threads.)

也许这是我的英语问题,但我在这个描述中看到了矛盾:
从第一段开始,我不明白大约到达顺序政策的含义

  1. 从第一段我了解到锁获取最旧的等待线程。如果最旧的线程 - 读取线程,那么它将是一组读取线程,其等待时间比等待时间最长的写入线程更长。
  2. 从第二段我了解到,如果等待集中存在写锁,则不会获取读锁。

请澄清这个矛盾。

最佳答案

在这里,引用您的引述:

或者如果有一个读者线程

换句话说:一个作家赢得了一个单一读者;但是当一群读者想要锁时,他们就会得到锁。

关于这个问题:“group 到底是什么意思”……这将是一个实现细节,只能通过查看 source code 才能获得。 .

关于java - 关于 ReentrantReadWriteLock 的文档矛盾。在公平模式下,最终写锁是否优先于读锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42803548/

相关文章:

mysql - 行级锁定在 rails/mysql 中无法按预期工作

java - 访问二维数组时出现 NullPointerException

Java - CountDownLatch.await() 可以由编译器重新排序吗

java - 为什么 Thread.stop() 方法使用不安全?

concurrency - sync.WaitGroup 不等待

c# - 自定义事务实现 - 线程和锁

.net - .NET中的分布式锁定

java - 编译 JAR 文件及其所有 JAR 依赖项

java - 选择时的 SWT 表格单元格前景

Java文本分析程序