java读写锁源码_【锁】ReentrantReadWriteLock之读写锁公平性体现源码分析

ReentrantReadWriteLock的公平性体现在Sync的两个子类NonfairSync和FairSync重写的Sync中的两个方法:

/**

* Returns true if the current thread, when trying to acquire

* the read lock, and otherwise eligible to do so, should block

* because of policy for overtaking other waiting threads.

* 如果当前线程在尝试获取读锁时阻塞,并且由于策略原因无法超过其他等待的线程,

* 因此有资格阻塞读锁,则返回true。

*/

abstract boolean readerShouldBlock();

/**

* Returns true if the current thread, when trying to acquire

* the write lock, and otherwise eligible to do so, should block

* because of policy for overtaking other waiting threads.

* 如果当前线程在尝试获取写锁时阻塞,并且由于策略原因无法超过其他等待的线程,

* 因此有资格这样做,则返回true。

*/

abstract boolean writerShouldBlock();

readerShouldBlock方法使用的地方为共享模式锁的获取,writerShouldBlock使用的地方为独占模式锁的获取。

NonfairSync

final boolean writerShouldBlock() {

// writers can always barge

//非公平模式下,写锁的获取,当发生阻塞的时候,返回false

return false;

}

写锁在非公平模式,在非重入的前提下永远获取锁失败。

final boolean readerShouldBlock() {

/* As a heuristic to avoid indefinite writer starvation,

* block if the thread that momentarily appears to be head

* of queue, if one exists, is a waiting writer. This is

* only a probabilistic effect since a new reader will not

* block if there is a waiting writer behind other enabled

* readers that have not yet drained from the queue.

* 作为一种避免无限writer饥饿的启发式方法,

* 如果暂时出现队列头的线程(如果存在)是一个正在等待的writer,则阻塞。

* 这只是一种概率效应,因为如果在其他启用的reader后面有一个等待的writer,

* 而这些reader还没有从队列中删除,则新reader不会阻塞。

*/

return apparentlyFirstQueuedIsExclusive();

}

final boolean apparentlyFirstQueuedIsExclusive() {

Node h, s;

return (h = head) != null &&

(s = h.next) != null &&

!s.isShared() &&

s.thread != null;

}

读锁在非公平模式下,同步队列中最少为两个节点,且当前节点不是共享模式,且节点的线程不为null,就阻塞获取锁。

FairSync

final boolean writerShouldBlock() {

return hasQueuedPredecessors();

}

final boolean readerShouldBlock() {

return hasQueuedPredecessors();

}

public final boolean hasQueuedPredecessors() {

// The correctness of this depends on head being initialized

// before tail and on head.next being accurate if the current

// thread is first in queue.

Node t = tail; // Read fields in reverse initialization order

Node h = head;

Node s;

return h != t &&

((s = h.next) == null || s.thread != Thread.currentThread());

}

公平模式下,读锁与写锁都要看自己是不是将要获取锁的同步队列节点,如果不是,就阻塞。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值