Inter Thread Communication - BEHIND JAVA

Inter Thread Communication

Share This

The inter thread communication can be implement with the help of methods like wait(), notify(), notifyAll().These all methods are comes under Object class. In inter thread communication what actually does is One thread produce and put something on shared bucket, and then tell other thread that there is an item for your interest in shared object, consumer thread than pick than item and do his job.

Also the wait(), notify(), notifyAll() methods can be only written inside a synchronized block which means only one thread can access those resource at a time. Here follows a sample program for queue processing using wait() and notifyAll().

The output becomes follows.

Integer 0 is Produced
Consumed Item is 0
Integer 2 is Produced
Consumed Item is 2
Integer 4 is Produced
Consumed Item is 4
Integer 6 is Produced
Consumed Item is 6
Integer 8 is Produced
Consumed Item is 8

No comments:

Post a Comment

Pages