V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenguoyu
V2EX  ›  Java

Reentrantlock 怎么知道当前获得锁的是哪个线程

  •  
  •   chenguoyu · 2020-04-12 17:47:12 +08:00 · 2329 次点击
    这是一个创建于 1473 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚刚面试官问了这个问题,没答上来,百度也没找到。
    3 条回复    2020-04-12 19:39:40 +08:00
    lhx2008
        1
    lhx2008  
       2020-04-12 17:56:06 +08:00 via Android
    应该是说说 AQS 的东西
    tcxurun
        2
    tcxurun  
       2020-04-12 18:49:32 +08:00   ❤️ 1
    Reentrantlock 主要是基于 AQS,而 AQS 继承一个抽象类 AbstractOwnableSynchronizer,有个变量 exclusiveOwnerThread,用来存储锁持有的线程,然后比较,比如源码中你会看到这个
    (current == getExclusiveOwnerThread())
    coer
        3
    coer  
       2020-04-12 19:39:40 +08:00
    从 lock()进去走几步就能看到 nonfairTryAcquire(int acquires)中的 else if (current == getExclusiveOwnerThread()).....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:01 · PVG 21:01 · LAX 06:01 · JFK 09:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.