ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

ReentrantLock源码之公平锁的实现

2022-05-14 11:04:42  阅读:199  来源: 互联网

标签:Node 查看 ReentrantLock 源码 公平 arg new public


  • 代码案例
public class ReentrantLockDemo {

    public static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock(true);
        reentrantLock.lock();
        reentrantLock.unlock();
    }

}
  • 查看无参构造方法
    public ReentrantLock() {
        sync = new NonfairSync();    // new了1个非公平实现
    }
  • 查看sync

  • 查看有参构造

    public ReentrantLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();    // 为true时返回公平实现,否则返回非公平实现
    }

  • ReentrantLock实现了Lock接口

  • 查看方法

  • 查看内部类

  • 查看lock方法

public class ReentrantLockDemo {

    public static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock(true);
        reentrantLock.lock();
        reentrantLock.unlock();
    }

}

public void lock() {
    sync.acquire(1);
}

public final void acquire(int arg) {
    // 尝试获取锁
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))    // 同时获取队列失败
        selfInterrupt();      // 则线程终止
}
  • 查看尝试获取锁的方法
    protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }
  • 查看非公平实现
static final class NonfairSync extends Sync {
    private static final long serialVersionUID = 7316153563782823691L;
    protected final boolean tryAcquire(int acquires) {
        return nonfairTryAcquire(acquires);
    }
}

@ReservedStackAccess
final boolean nonfairTryAcquire(int acquires) {
    final Thread current = Thread.currentThread();    // 获取当前线程
    int c = getState();    // 获取状态
    if (c == 0) {    // 当前没有线程获取到这个锁
        if (compareAndSetState(0, acquires)) {
            setExclusiveOwnerThread(current);
            return true;    // 获得锁
        }
    }
    else if (current == getExclusiveOwnerThread()) {    // 是否是持有锁的线程,即重入状态
        int nextc = c + acquires;    // 重入状态加1
        if (nextc < 0) // overflow      // 超过最大重入状态,抛出异常
            throw new Error("Maximum lock count exceeded");
        setState(nextc);    // 更新
        return true;    // 获得锁
    }
    return false;
}
  • 查看addWaiter方法
    public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
  • 查看EXCLUSIVE

  • 查看addWaiter方法

    private Node addWaiter(Node mode) {
        Node node = new Node(mode);    // 传入参数,当前线程,new1个Node

        for (;;) {
            Node oldTail = tail;    // 前置节点指向尾节点
            if (oldTail != null) {
                node.setPrevRelaxed(oldTail);
                if (compareAndSetTail(oldTail, node)) {    // node设置为尾节点
                    oldTail.next = node;
                    return node;
                }
            } else {
                initializeSyncQueue();    // 若整条队列为null,执行入队
            }
        }
    }
  • 查看Node
Node(Node nextWaiter) {
    this.nextWaiter = nextWaiter;
    THREAD.set(this, Thread.currentThread());
}

标签:Node,查看,ReentrantLock,源码,公平,arg,new,public
来源: https://www.cnblogs.com/chniny/p/16269335.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有