I’m doing a concurrency problem homework. I have many threads doing specific things and a method which decides when these threads can access a resource.
So my class looks something like this:
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.*/ }
I’m getting the error:
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)
I’ve checked the documentation and it says that getWaitQueueLength throws IllegalArgumentException when “the given condition is not associated with this lock” but from what I understand, it is associated in my code. Can someone help me?
Answer
When you invoke
getWaitQueueLength(canTakeOff)
you are calling
this.getWaitQueueLength(canTakeOff)
where this is your instance of Boss. Despite canTakeOff has been declared has a member, it doesn’t belong to Boss, it is a Condition obtained from access
I think that you must change the aforementioned line a bit and use
access.getWaitQueueLength(canTakeOff)
More info: Javadoc getWaitQueueLength throws IllegalArgumentException