当前位置: 移动技术网 > IT编程>开发语言>Java > 一分钟理解Java公平锁与非公平锁

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

2019年12月11日  | 移动技术网IT编程  | 我要评论

本人免费整理了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状态,可以执行开始,到实际线程执行是要比较久的时间的。

而且,在一个锁释放之后,其他的线程会需要重新来获取锁。其中经历了持有锁的线程释放锁,其他线程从挂起恢复到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()方法,以排他的方式来获取锁,成功了立刻返回,否则将线程加入队列,知道成功调用为止。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网