The moose likes Threads and Synchronization and the fly likes notify Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "notify" Watch "notify" New topic
Author

notify

Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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?


----------------------------------<br />SCJP 1.4
Enrique Pedraza
Greenhorn

Joined: Mar 14, 2003
Posts: 4
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.


not so long, for the sun to rise
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1158

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

Joined: Feb 23, 2001
Posts: 104
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.
 
 
subject: notify
 
Threads others viewed
thread question
wait and notify
Threads
Interview q
Regarding Thread class
developer file tools