公平锁与非公平锁

ReentrantLock中很明显能够看到其中同步包括两种,分别是公平的FairSync和非公平的NonfairSync。公平锁的做用就是严格按照线程启动的顺序来执行的,不容许其余线程插队执行的;而非公平锁是容许插队的。java

默认状况下ReentrantLock是经过非公平锁来进行同步的,包括synchronized关键字都是如此,由于这样性能会更好。由于从线程进入了RUNNABLE状态,能够执行开始,到实际线程执行是要比较久的时间的。并且,在一个锁释放以后,其余的线程会须要从新来获取锁。其中经历了持有锁的线程释放锁,其余线程从挂起恢复到RUNNABLE状态,其余线程请求锁,得到锁,线程执行,这一系列步骤。若是这个时候,存在一个线程直接请求锁,可能就避开挂起到恢复RUNNABLE状态的这段消耗,因此性能更优化。markdown

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

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

/**
     * 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();
    }

下面参考一下FairSyncNonfairSynclock方法的实现性能

/** * 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()方法,以排他的方式来获取锁,成功了马上返回,不然将线程加入队列,知道成功调用为止。优化

相关文章
相关标签/搜索