Home Javatutorials Interthread Communication in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Interthread Communication in Java

Advertisement
Interthread Communication in Java

Interthread Communication in Java

As you know about producer-consumer problem of thread. In this problem one thread is producing data and another is consuming it, This problem become worst when produce needs to wait until the consumer is finished.

In a polling system, many CPU cycle is wasted by the consumer while waiting for the producer to produce. After finishing producer, producer wait for the consumer to finish. This would waste more CPU cycles.

To avoid wasting of cycle or polling, interthread communication is a good way.  Given below some function helpful in interthread communication :

Methods Description
wait( ) This method tells the calling thread to give up the monitor and go to sleep until some other
thread enters the same monitor and calls notify( ).
notify( ) This method wakes up the first thread that called wait( ) on the same object.
notifyAll( )  This method wakes up all the threads that called wait( ) on the same object.c The highest
priority thread will run first.

EXAMPLE

In the given below Example, it has four classes. The 'Queue' class is the class which we are trying to synchronize. 'Producer' class are producing queue entries. 'Consumer' Class consuming queue entries. And the class 'InterthreadDemo' which create 'Producer', and 'Consumer' and 'Queue'. Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue. Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it.

package corejava;

class Queue {
int n;
boolean valueSet = false;

synchronized int get() {
if (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}

synchronized void put(int n) {
if (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}

class Producer implements Runnable {
Queue q;

Producer(Queue q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {
int i = 0;
while (true) {
q.put(i++);
}
}
}

class Consumer implements Runnable {
Queue q;

Consumer(Queue q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {
while (true) {
q.get();
}
}
}

public class InterthreadDemo {
public static void main(String args[]) {
Queue q = new Queue();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java InterthreadDemo
Press Control-C to stop.
Put: 0
Got: 0
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3

The above given output is terminated forcibly by pressing Ctrl-C key.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: Interthread Communication in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com