Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait & notify

 
Poornachandran R
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone explain why wait() and notify() methods are defined in Object rather than Thread ?
Poorna
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks,
Cindy
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this previous thread. And some good comments here.
[ July 17, 2003: Message edited by: Jim Yingst ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jim for join()-ing the discussion threads.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic