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:
[code]
public interface IReadWriteLock{
public void lockWrite();
public void unlockWrite();
public void lockRead();
public void unlockRead();
}
[/code]
This interface would then be implemented by a class:
[code]
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();
}
}
[/code]
Now, in order to force synchronization , I use the aspect:
[code]
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;
}
}
[/code]
The point is, every time I try to execute something, I get an IllegalMonitorStateException. In this link:
http://stackoverflow.com/questions/1537116/illegalmonitorstateexception-on-wait-callThey say that the thread must own the monitor, but I don't see where that is not happening. Can some one help?