带你快速了解Java锁中的公平锁与非公平锁

前言java

Java语言中有许多原生线程安全的数据结构,好比ArrayBlockingQueue、CopyOnWriteArrayList、LinkedBlockingQueue,它们线程安全的实现方式并不是经过synchronized关键字,而是经过java.util.concurrent.locks.ReentrantLock来实现。程序员

v2-b4536744e0accf8e47573c091a3795b8_hd.png

锁的底层实现面试

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

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

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

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

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

v2-6e927b40a681edf06c4a0f1e4b3f1716_hd.png

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

    /**
     * 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()方法,以排他的方式来获取锁,成功了马上返回,不然将线程加入队列,知道成功调用为止。欢迎你们关注个人公种浩【程序员追风】,2019年多家公司java面试题整理了1000多道400多页pdf文档,文章都会在里面更新,整理的资料也会放在里面。

v2-74fd5e720aa55bdda2630701913c3a9a_hd.png

总结

上锁的过程自己也是有时间开销的,若是操做资源的时间比上锁的时间还短建议使用非公平锁能够提升系统的吞吐率;不然就老老实实的用公平锁。


最后

欢迎你们一块儿交流,喜欢文章记得点个赞哟,感谢支持!

相关文章
相关标签/搜索