gpt4 book ai didi

java - getWaitQueueLength 抛出 IllegalArgumentException : Not owner

转载 作者:行者123 更新时间:2023-12-02 03:48:23 26 4
gpt4 key购买 nike

我正在做并发问题作业。我有许多线程执行特定的操作,并且有一个方法决定这些线程何时可以访问资源。

所以我的类看起来像这样:

public class Boss extends ReentrantLock implements Runnable {

Lock access = new ReentrantLock();
Condition canTakeOff = access.newCondition();
Condition canLand = access.newCondition();

public void accessToLanding(){
access.lock();
try{
if(getWaitQueueLength(canTakeOff) > 0){
canTakeOff.notifyAll();
} else if {
/* some other cases */
}
} catch (InterruptedException e){
e.printStackTrace();
} finally {
access.unlock();
}
}

public void run(){
accessToLanding();
}

/* Methods which are called by objects of a different class,
they are awaiting for the signal from accessToLanding.*/
}

我收到错误:

Exception in thread "Thread-0" java.lang.IllegalArgumentException: Not owner
at java.util.concurrent.locks.AbstractQueuedSynchronizer.getWaitQueueLength(AbstractQueuedSynchronizer.java:1789)
at java.util.concurrent.locks.ReentrantLock.getWaitQueueLength(ReentrantLock.java:720)

我检查了文档,它说当“给定条件与此锁无关”时, getWaitQueueLength 会抛出 IllegalArgumentException ,但根据我的理解,它与我的代码相关联。有人可以帮助我吗?

最佳答案

当你调用时

getWaitQueueLength(canTakeOff)

你正在打电话

this.getWaitQueueLength(canTakeOff)

其中this是您的Boss实例。尽管canTakeOff已声明有成员,但它并不属于 Boss,而是从access获取的条件

我认为您必须稍微更改上述行并使用

access.getWaitQueueLength(canTakeOff)

更多信息:Javadoc getWaitQueueLength throws IllegalArgumentException

关于java - getWaitQueueLength 抛出 IllegalArgumentException : Not owner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com