• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify  RSS feed

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is it only thread objects that can issue the notify command to a thread which is currently in waiting state?
I have two classes, one is a normal object and the other is a thread object, in this part of the code from the normal class, is it possible to use a notify command to tell the thread object to start executing again?
 
Enrique Pedraza
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify(), notifyAll() and wait() may only be invoked on lock objects in a synchronized block, that is:
synchronized (o)
{
o.notifyAll();
}
this will wake up all threads which invoked o.wait() in any synchronized block whith the same lock object.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify() can be called on any Object. A thread is not an object. You are kind of mixing the two terms.
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify() can be called only by the thread which is current lock owner of this particular object.
Since notify() is in Object class, every Java object has this method, but it can be called only from synchronized block of code, otherwise an exception will be thrown (I guess it is IllegalMonitorState exception with the message "Current Thread not owner").
So, in plain English: you have to lock object first then call notify() or wait(). Locking is done by threads, not objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!