Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » IllegalMonitorStateException using locks
IllegalMonitorStateException using locks [message #1052978] Wed, 01 May 2013 17:05 Go to next message
Pedro Miguel Pereira Serrano Martins is currently offline Pedro Miguel Pereira Serrano MartinsFriend
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?
Re: IllegalMonitorStateException using locks [message #1052988 is a reply to message #1052978] Wed, 01 May 2013 17:59 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
I don't know how your RWPriorityMonitorLock gets instantiated, so I can't tell why it's not working. Regardless, I'd recommend moving to a different aspect isntantiation model here.

http://www.eclipse.org/aspectj/doc/released/progguide/semantics-aspects.html

I'd go either with perThis(IReadWriteLock) in order to avoid the allLocks field in the aspect. But, this would not address your threading problem. Or, I would go with perCFlow(SomeObject.someMethod(..)) to ensure that you always use the same lock on each call to someMethod (this will be implementation specific).
Re: IllegalMonitorStateException using locks [message #1053226 is a reply to message #1052978] Fri, 03 May 2013 08:57 Go to previous messageGo to next message
Pedro Miguel Pereira Serrano Martins is currently offline Pedro Miguel Pereira Serrano MartinsFriend
Messages: 5
Registered: April 2013
Junior Member
Hey, thanks, but the first answer is indicated for the Singleton pattern (from what I read) and I wouldn't understand the need for the second. What troubles me is that if I simply remove the advices and copy-paste it's code into the methods everything works fine and I have no idea why =(
Re: IllegalMonitorStateException using locks [message #1053283 is a reply to message #1053226] Fri, 03 May 2013 14:43 Go to previous message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
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.
Previous Topic:A simple parametrized class question
Next Topic:AspectJ AOP config with JSP
Goto Forum:
  


Current Time: Thu Apr 25 04:13:46 GMT 2024

Powered by FUDForum. Page generated in 0.02870 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top