• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can some one explain this..

 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expert from Khalid Mughal's book:

"waiting and notifying are means of communication between threads that synch on the same object.These methods can only be executed on an object whose lock the thread holds, otherwise it will result in an IllegalMonitorStateException"

Can some one gimme an example so that i can understand this better?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
=======================================================================
"waiting and notifying are means of communication between threads
that synch on the same object. These methods can only be executed
on an object whose lock the thread holds, otherwise it will result
in an IllegalMonitorStateException"
=======================================================================

Thr r two threads threadA & threadB, holding lock on the same object 'obj'.
Assume at this instant threadA is excuting obj, as soon it is done/ or its time slice completes; It need to intimate threadB to PROCEED with obj, for this threadA calls notify() on obj, in turn obj wll intimate its threads to proceed with further execution.

A thread not concerned with an object (ie not holding a lock on it), cant command the object for intimation. If this happens an 'IllegalMonitorStateException' is thrown.

" i cnt call my friends(sm othr thread) gal(sm other object) to do good things (to perform sm action) bcoz the THREAD is with my friend(the othr thread). "

HOPE THIS MAKE SENSE.



 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Thread can call wait or notify on an object whose lock the thread has.

when does a Thread have an object lock?
when it enters a synchronized block of code.

Therefore, an IllegalMonitorStateException is thrown when a Thread encounters a wait/notify method which is not in a synchronized block (i.e the Thread doesn't have the lock!)

Lets make it clearer
What exactly does the wait method do? It commands the Thread executing that code to give up the lock on the object it is holding. How can a Thread give up the lock when it has none? Therefore an IllegalMonitorStateException is thrown. But if it were a synchronized block of code, the Thread executing that block has the object lock and therefore is capable of giving it up.
And similar is the case with notify.

Imagine this:
There is a circle of people and one baton. Only the person who holds the baton may speak, all the rest must wait for the person to pass the baton.
I can say that " I'm giving up the baton" (by saying wait() ) only if I have the baton. If I don't, then the referee will say "you idiot, you don't have the baton and you can't speak, so shut up until you get the baton." But if I had it, I could say "ok, i'm done with this, who wants to speak next may take it" and that would be fine.

And Navneet, two objects cannnot hold the lock for the same object at the same time. That is the exact use of lock, only one thread may have it at one point of time, all the other threads that need to access that locked object will have to "wait" until the thread holding the lock relinquishes it.

[ August 27, 2005: Message edited by: Akshay Kiran ]
[ August 27, 2005: Message edited by: Akshay Kiran ]
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, Mike Corleone seems like you've popped out of Godfather- Mario Puzo and all the time I was thinking it sounded so familiar.
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He He He.. Thanks for that AK
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic