IllegalMonitorStateException using locks [message #1052978] |
Wed, 01 May 2013 13:05  |
Eclipse User |
|
|
|
Hellow, I have a code that uses multiple threads, and so I need read and write locks for it. Simply put, this is what I have so far:
public interface IReadWriteLock{
public void lockWrite();
public void unlockWrite();
public void lockRead();
public void unlockRead();
}
This interface would then be implemented by a class:
public class RWPriorityMonitorLock implements IReadWriteLock{
private boolean dbWritting, writterWantsIn;
private int numReaders;
public RWPriorityMonitorLock() {
dbWritting = false;
numReaders = 0;
writterWantsIn = false;
}
@Override
public void lockRead() {
while(dbWritting || writterWantsIn)
try{
wait();
}catch(InterruptedException e){ e.printStackTrace(); }
numReaders++;
}
@Override
public void unlockRead() {
numReaders--;
if(numReaders == 0)
notify();
}
@Override
public void lockWrite() {
writterWantsIn = true;
while(numReaders > 0 || dbWritting)
try{
wait();
}catch(InterruptedException e){ e.printStackTrace(); }
dbWritting = true;
writterWantsIn = false;
}
@Override
public void unlockWrite() {
dbWritting = false;
notifyAll();
}
}
Now, in order to force synchronization , I use the aspect:
public aspect IReadWriteLockAspect {
private static Map<IReadWriteLock, Lock> allLocks = new HashMap<>();
pointcut lockReflectInit(): call(public Object
java.lang.reflect.Constructor.newInstance(..));
pointcut hardWaitMutex():
call(public void IReadWriteLock+.*());
void around(): hardWaitMutex(){
IReadWriteLock currentLock = (IReadWriteLock) thisJoinPoint.getTarget();
Lock mutex = allLocks.get(currentLock);
mutex.lock();
try{
proceed();
}finally{
mutex.unlock();
}
}
Object around(): lockReflectInit() {
IReadWriteLock lockType = (IReadWriteLock)proceed();
allLocks.put(lockType, new ReentrantLock(true));
return lockType;
}
}
The point is, every time I try to execute something, I get an IllegalMonitorStateException. In this link:
questions/1537116/illegalmonitorstateexception-on-wait-call @ stackovweflow
They say that the thread must own the monitor, but I don't see where that is not happening. Can some one help?
|
|
|
|
|
Re: IllegalMonitorStateException using locks [message #1053283 is a reply to message #1053226] |
Fri, 03 May 2013 10:43  |
Eclipse User |
|
|
|
Well, based on the exception you are getting, there is a lock being used outside of the thread that it was created in. Or perhaps, it is getting locked on one thread and unlocked on another.
I'd make sure that each RWPriorityMonitorLock instance only operates in a single thread.
|
|
|
Powered by
FUDForum. Page generated in 0.02490 seconds