IllegalMonitorStateException using locks [message #1052978] |
Wed, 01 May 2013 17:05 |
Pedro Miguel Pereira Serrano Martins Messages: 5 Registered: April 2013 |
Junior Member |
|
|
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?
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02870 seconds