File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Help please Monitor exception with lock/unlock see code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Help please Monitor exception with lock/unlock see code" Watch "Help please Monitor exception with lock/unlock see code" New topic
Author

Help please Monitor exception with lock/unlock see code

Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
//contains locked records
private Vector r_lock = new Vector(); //Global declaration

public void lock(int record) throws IOException {
synchronized(r_lock)

{

while(r_lock.contains(new Integer(record)))
{
try
{
wait();
}
catch(InterruptedException e)
{
throw new IOException("lock failed");
}
}
r_lock.add(new Integer(record));
}


}
/**
* Unlock the requested record. Ignored if the caller does not have
* a current lock on the requested record.The record that was locked
* is removed from the Vector r_lock and all threads are notifyed.
*/
public void unlock(int record) {
synchronized(r_lock)
{
if(r_lock.contains(new Integer(record)))
{
r_lock.remove(new Integer(record));

}
notifyAll();
}
}

here is my error any help greatly appreciated
Exception occurred during event dispatching:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at suncertify.db.Data.unlock(Data.java:496)
at FlyByNight.BookSeats(fbn.java, Compiled Code)
at FlyByNight.actionPerformed(fbn.java:850)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:10
66)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1101)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:204)
at java.awt.Component.processMouseEvent(Component.java:3165)
at java.awt.Component.processEvent(Component.java, Compiled Code)
at java.awt.Container.processEvent(Container.java, Compiled Code)
at java.awt.Component.dispatchEventImpl(Component.java, Compiled Code)
at java.awt.Container.dispatchEventImpl(Container.java, Compiled Code)
at java.awt.Component.dispatchEvent(Component.java, Compiled Code)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java, Com
piled Code)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java, Comp
iled Code)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java, Compiled
Code)
at java.awt.Container.dispatchEventImpl(Container.java, Compiled Code)
at java.awt.Window.dispatchEventImpl(Window.java, Compiled Code)
at java.awt.Component.dispatchEvent(Component.java, Compiled Code)
at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)
at java.awt.EventDispatchThread.pumpOneEventForComponent(EventDispatchTh
read.java, Compiled Code)
at java.awt.EventDispatchThread.pumpEventsForComponent(EventDispatchThre
ad.java:95)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:90)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Matt Cannata
Greenhorn

Joined: Mar 23, 2001
Posts: 23
The problem is that when you call notifyAll() in the unlock method, you're really calling this.notifyAll(), where this is the object that contains the unlock method. Since your're method synchronizes on r_lock, you don't own the lock to this when you call notifyAll(). What you need to do is call r_lock.notifyAll(), which will release the lock on the r_lock Vector, and notify all that are waiting on it.
Matt
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Thanks you very much it works great
Matt I have another question The requirements state if a -1 is passed to the lock method in Data lock(int)
to lock the whole database How would I do this and How to unlock the whole database Thanks Lisa
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help please Monitor exception with lock/unlock see code
 
Similar Threads
lock and unlock
IllegalMonitorStateException
Locking the whole Data base if -1 see code????
How to test lockmanager?!!
IllegalMonitorStateException