This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt regarding monitor object and IllegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding monitor object and IllegalMonitorStateException" Watch "Doubt regarding monitor object and IllegalMonitorStateException" New topic
Author

Doubt regarding monitor object and IllegalMonitorStateException

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Have a look at the following code snippet:



Question is what will happen when we execute the code?

1.) This program will wait for ever

2.) This program will throw an InterruptedException at runtime

3.) This program will throw an IllegalMonitorStateException at runtime

4.) This program will not compile

5.) None of the above

The answer is option 3.

My knowledge about concurrency is not that good. So I tried running the code in eclipse. It throws a RuntimeException of type IllegalMonitorStateException. I have a few queries regarding the flow in this code:

a) First of all what is the monitor of an object in Java? Is it a state or an attribute of the object? How is it different from lock of an object?

b) I read the Java specification for IllegalMonitorStateException. It says that this type of RuntimeException is Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor. What is meant by the statement "one thread has attempted to wait on an object's monitor or notify other threads waiting on an object's monitor without owning the monitor "? In the above example, which thread is trying to call wait/notifyAll on which thread without owning its monitor and ,hence ,causing the said exception to be thrown?


~ Mansukh
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Mansukhdeep Thind wrote:a) First of all what is the monitor of an object in Java? Is it a state or an attribute of the object? How is it different from lock of an object?
They are the same. We talk about an object's lock, but the documentation talks about the object's monitor. What they both mean is the same thing: the sentinel lock that allows access through to only one thread at a time.

Mansukhdeep Thind wrote:b) I read the Java specification for IllegalMonitorStateException. It says that this type of RuntimeException is Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor. What is meant by the statement "one thread has attempted to wait on an object's monitor or notify other threads waiting on an object's monitor without owning the monitor "?

It means precisely what it says. It means code in one thread attempts to call the wait() method or notify() method or notifyAll() method on an object without first claiming the lock (owning the monitor). You claim an object's lock by using the synchronized(object) construct or calling a synchronized method from an object.

In the above example, which thread is trying to call wait/notifyAll on which thread without owning its monitor and ,hence ,causing the said exception to be thrown?

Which thread is calling an object's wait(), notify(), or notifyAll() method? Then, determine which objects' monitors that thread owns (by tracking what synchronized blocks the thread enters). Then see if any thread calls one of those methods without first entering a block synchronized on the object.


Steve
 
GeeCON Prague 2014
 
subject: Doubt regarding monitor object and IllegalMonitorStateException