Powered by Blogger.

Interthread Communication

No comments :
Java includes an elegant Interprocess communication mechanism via the wait(), notify() and notifyAll() methods.These methods are implemented as final method in object, so all classes have them. all three methods can be called only from within a synchronized context. Although conceptually advanced from a computer science perspective, the rules for using these methods are actually quite simple:
wait(): Tells the calling thread to given up the monitor and go to sleep until some other thread enters the same monitor and calls notify() or notifyAll().
notify(): wakes up a thread that called wait() on the same object.
notifyAll(): wakes up a thread that called wait() on the same object. One of the threads will be granted access.

These methods are declared within Object, as shown here:

The proper way to write this program in java is to use wait() and notify() to signal in both directions, shown here:

Output:

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.