V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
chenguoyu
V2EX  ›  Java

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

  •  
  •   chenguoyu · Apr 12, 2020 · 3054 views
    This topic created in 2212 days ago, the information mentioned may be changed or developed.
    刚刚面试官问了这个问题,没答上来,百度也没找到。
    3 replies    2020-04-12 19:39:40 +08:00
    lhx2008
        1
    lhx2008  
       Apr 12, 2020 via Android
    应该是说说 AQS 的东西
    tcxurun
        2
    tcxurun  
       Apr 12, 2020   ❤️ 1
    Reentrantlock 主要是基于 AQS,而 AQS 继承一个抽象类 AbstractOwnableSynchronizer,有个变量 exclusiveOwnerThread,用来存储锁持有的线程,然后比较,比如源码中你会看到这个
    (current == getExclusiveOwnerThread())
    coer
        3
    coer  
       Apr 12, 2020
    从 lock()进去走几步就能看到 nonfairTryAcquire(int acquires)中的 else if (current == getExclusiveOwnerThread()).....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2535 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 02:40 · PVG 10:40 · LAX 19:40 · JFK 22:40
    ♥ Do have faith in what you're doing.