Saturday, June 26, 2010

Object Class Methods

Why wait() and notify() are used only for threads, why not to objects?

The wait() and notify() methods are object-specific. The wait() method suspends the current thread of execution, and tells the object to keep track of the suspended thread. The notify() method tells the object to wake up the suspended threads that it is currently keeping track of. Since wait() and notify() are object specific, they must be used within code that is synchronized on the object in question.

It is also important to use state variables when using wait() and notify(), as threads can be woken up by conditions other than notify().


2) Because in object we dont want our result in syncronized way but in case of threads the result should be syncronized so we use wait and notify.


3) In threads for interprocess communication between threads is done using wait() notify()
methods because to avoid polling problem. But in case of objects interprocessing communication between objects is done automatically while processing program in JVM.


4) wait() and notify() are NOT needed for objects statements/method calls within the same thread because execution on objects running in the same thread will run one at a time and not overlap.

If execution happens in different threads then there is a chance for them to overlap and the need for mechanism to manage concurrent execution arise.

No comments:

Post a Comment