一分钟理解Java公平锁与非公平锁

本人免费整理了Java高级资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G,须要本身领取。
传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q并发

 

和朋友聊天他提到:ReentrantLock 的构造函数能够传递一个 bool 数据,true 时构造的是“公平锁”、false 时构造的是“非公平锁”。个人印象中锁是不区分类型的,因此认为这应该是 Java 发明的概念,因而就恶补了一下。分布式

锁的底层实现

不管什么语言在操做系统层面锁的操做都会变成系统调用(System Call),以 Linux 为例,就是 futex 函数,能够把它理解为两个函数: futex_wait(s),对变量 s 加锁;futex_wake(s)释放 s 上的锁,唤醒其余线程。函数

在ReentrantLock中很明显能够看到其中同步包括两种,分别是公平的FairSync和非公平的NonfairSync。高并发

公平锁的做用就是严格按照线程启动的顺序来执行的,不容许其余线程插队执行的;而非公平锁是容许插队的。性能

 

默认状况下ReentrantLock是经过非公平锁来进行同步的,包括synchronized关键字都是如此,由于这样性能会更好。优化

由于从线程进入了RUNNABLE状态,能够执行开始,到实际线程执行是要比较久的时间的。ui

并且,在一个锁释放以后,其余的线程会须要从新来获取锁。其中经历了持有锁的线程释放锁,其余线程从挂起恢复到RUNNABLE状态,其余线程请求锁,得到锁,线程执行,这一系列步骤。若是这个时候,存在一个线程直接请求锁,可能就避开挂起到恢复RUNNABLE状态的这段消耗,因此性能更优化。spa

/** * Creates an instance of {@code ReentrantLock}. * This is equivalent to using {@code ReentrantLock(false)}. */
    public ReentrantLock() { sync = new NonfairSync(); }

 

默认状态,使用的ReentrantLock()就是非公平锁。再参考以下代码,咱们知道ReentrantLock的获取锁的操做是经过装饰模式代理给sync的。操作系统

/** * Acquires the lock. * * <p>Acquires the lock if it is not held by another thread and returns * immediately, setting the lock hold count to one. * * <p>If the current thread already holds the lock then the hold * count is incremented by one and the method returns immediately. * * <p>If the lock is held by another thread then the * current thread becomes disabled for thread scheduling * purposes and lies dormant until the lock has been acquired, * at which time the lock hold count is set to one. */
    public void lock() { sync.lock(); }

 

下面参考一下FairSync和NonfairSync对lock方法的实现:线程

/** * Sync object for non-fair locks */
    static final class NonfairSync extends Sync { /** * Performs lock. Try immediate barge, backing up to normal * acquire on failure. */
        final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } } /** * Sync object for fair locks */
    static final class FairSync extends Sync { final void lock() { acquire(1); } }

 

当使用非公平锁的时候,会马上尝试配置状态,成功了就会插队执行,失败了就会和公平锁的机制同样,调用acquire()方法,以排他的方式来获取锁,成功了马上返回,不然将线程加入队列,知道成功调用为止。

相关文章
相关标签/搜索