java 在没有 IllegalMonitorStateException 的情况下解锁 ReentrantLock

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2811236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:58:16  来源:igfitidea点击:

Unlock on ReentrantLock without IllegalMonitorStateException

javaconcurrencylocking

提问by Mikhail

I have a piece of code (simplified):

我有一段代码(简化):

if(reentrantLockObject.isLocked()) {
       reentrantLockObject.unlock();
}

where reentrantLockObject is java.util.concurrent.locks.ReentrantLock. Sometimes I get IllegalMonitorStateException. It seams that lock was released between check and unlock() call. How can I prevent this exception?

其中 reentrantLockObject 是 java.util.concurrent.locks.ReentrantLock。有时我会收到 IllegalMonitorStateException。它接缝在 check 和 unlock() 调用之间释放了锁。我怎样才能防止这个异常?

回答by Jon Skeet

isLockedreturns whether anythread holds the lock. I think you want isHeldByCurrentThread:

isLocked返回是否有任何线程持有锁。我想你想要isHeldByCurrentThread

if (reentrantLockObject.isHeldByCurrentThread()) {
    reentrantLockObject.unlock();
}

Having said that, isHeldByCurrentThreadis documented to be mainly for diagnostic purposes - it would be unusual for this piece of code to be the right approach. Can you explain why you think you need it?

话虽如此,isHeldByCurrentThread已记录为主要用于诊断目的 - 这段代码是正确的方法是不寻常的。你能解释为什么你认为你需要它吗?

回答by John Vint

You need to own the lock to be able to unlock it. reentrantLockObject.isLocked() only is true if some thread owns the lock, not necessarily you.

您需要拥有锁才能解锁它。reentrantLockObject.isLocked() 仅当某个线程拥有锁时才为真,不一定是你。

  reentrantLockObject.lock();
  try{

       // do stuff
  }finally{
         reentrantLockObject.unlock();
  }

Here the thread owns the lock so they are able to unlock it.

这里线程拥有锁,因此他们能够解锁它。

回答by David Lekishvili

ReentrantLockthrows this exception according to this logic:

ReentrantLock根据这个逻辑抛出这个异常:

if (Thread.currentThread() != getExclusiveOwnerThread()) {
  throw new IllegalMonitorStateException();
}

So the solution is to check if the same thread is unlocking:

所以解决方案是检查是否是同一个线程正在解锁:

if (reentrantLockObject.isHeldByCurrentThread()) {
  reentrantLockObject.unlock();
}