-1

Possible Duplicate:
Why are wait() and notify() declared in Java's Object class?

I was wondering why notify(), notifyAll() and wait() methods are present in Object class, whereas other thread related methods are present in Thread class in Java ?

Community
  • 1
  • 1
Chethan kumar
  • 211
  • 2
  • 7

3 Answers3

1

Threads can use Objects to transmit messages from one thread to another, and these methods allow that to happen. A Thread calls wait() to say "I am waiting for a message to be sent to this object." Another thread can call notify() to say "I am sending a message to that object." The Object is therefore a conduit through which threads communicate without explicitly referencing each other. If the methods were in the Thread class, then two threads would need to have references to one another to communicate. Instead, all communicating threads just need to agree to use some specific shared resource.

[from http://www.coderanch.com/how-to/java/WaitAndNotifyInObjectClass]

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
naresh
  • 2,113
  • 20
  • 32
0

Because these methods work with the lock that is on an Object; it doesn't matter what thread uses the lock. Because they manipulate a property of an Object, they belong on the Object class.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

This is related to synchronization and locks.

We can synchronize on every object and the methods are used to interact with threads that are waiting on this objects monitor.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268