我正在做一个并发问题作业。我有许多线程在做特定的事情,还有一个方法决定这些线程何时可以访问资源。
所以我的课看起来像这样:
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)
这是您的老板实例。尽管canTakeOff已被声明具有成员,但它不属于Boss,它是从访问获得的条件
我认为您必须稍微更改上述内容并使用
access.getWaitQueueLength(canTakeOff)
更多信息:Javadoc getWaitQueueLength throws IllegalArgumentException
关于java - getWaitQueueLength引发IllegalArgumentException:不是所有者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36118178/