This section contains the detail about the Interthread Communication in java.
Interthread Communication
In a classic queuing problem, the data produced by one thread is consumed by another Thread.
In some cases , producer has to wait until the consumer has finished. Before finishing it generates more data. In a polling system, many CPU cycle is wasted by the consumer while waiting for the producer to producer. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, we use interprocess or interthread communication through the given below methods :
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. |
All three methods can be called only from within a synchronized context.
Example :
Description of the Program
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 single 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.
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.
[ 0 ] Comments