File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes wait & notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait & notify" Watch "wait & notify" New topic

wait & notify

Poornachandran R
Ranch Hand

Joined: Sep 11, 2002
Posts: 47
Can anyone explain why wait() and notify() methods are defined in Object rather than Thread ?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Because wait is called on objects not on Threads. When wait is called the Thread that owns the objects monitor gives it up and waits and watches to see when that monitor is notified. Then the Thread can try to re-gain ownership of the monitor.
Moving to the Threads Forum.
And Poornachandran R,
Welcome to JavaRanch!! Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
See also this previous thread. And some good comments here.
[ July 17, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Jim for join()-ing the discussion threads.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am going to naively propose a reason why wait and notify are methods of Object instead of Thread.
Every object has a wait set. When a thread invokes wait on an object, the thread is added to the object�s wait set. When another thread invokes notify on an object, a thread is removed from the object�s wait set.
Why not say Thread.wait(object)? Add the current thread to the wait set of object. Why not say Thread.notify(object)? Remove some thread from the wait set of object.
People who teach concurrency first talk about semaphores, then they talk about monitors*. I am guessing some important synchronization problems** have been studied using these two programming language constructs.
A Java object has the properties of a monitor. A Java object has a lock for synchronization, wait and signal procedures and condition variables***. Therefore, the language can take advantage of all the literature and concurrency solutions developed for monitors.
* monitors as formalized by Hoare 1974
** e.g. bounded buffers, readers/writers, sleeping barber, dining philosphers
*** a queue of delayed processes == wait set
[ July 17, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is another way to think about this question.
Synchronization is the principal semantic challenge for concurrent programs sharing memory.
One commonly sees two forms of synchronization: mutual exclusion and condition synchronization. Mutual exclusion ensures that only one thread is executing a critical section of code at a give point in time. Condition synchronization ensures that a given thread does not proceed until some specific condition holds.
There are 2 kinds of synchronization mechanisms: busy-wait synchronization and scheduler-based synchronization. There are three common forms of scheduler-based synchronization: semaphores, monitors and conditional critical regions.
Java might be said to be a blend of the features of monitors and conditional critical regions. Those features are found in Java objects.
In particular, the wait and notify methods of Java objects are Java�s implementation of the wait and signal procedures of monitors.
(credits: Michael Scott, Programming Language Concepts)
I guess what I am trying to say is, to understand this question we have to see the context.
I agree. Here's the link:
subject: wait & notify
jQuery in Action, 3rd edition