Saturday 28 September 2019

interthread communication in java - Online Help

Interthread communication:

let's discuss the important topic of Java multithreading is interthread communication in java.

Inter thread communication is used to allow synchronized threads to communicate with each other. Before we discuss inter-thread communication in java, we should know how and why this concept is included in java?

Multithreading replaces event loop programming by dividing your tasks into discrete and logical units. Thread also provides a secondary benefit: they avoid polling. Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, the appropriate action is taken. This waste CPU time.

For example,
Consider the queuing problem when one thread is proceeding some data and another is consuming it. The proceeding thread has to wait until the consuming thread is finished before it generates more data.

Also read: Thread in java



In a polling system, the consuming thread would waste many CPU cycles while it waited for the preceding thread to produce. if the proceeding thread is generated more data until then consuming thread has to wait. clearly this situation is undesirable.

To avoid polling, Java includes an elegant inter-process communication mechanism via wait(), notify(), and notifyAll() method. These methods are implemented as a final method in an object so that all classes have them. All three methods can be called only from within a synchronized method.

wait(): 

Tell the calling thread to give up the monitor and go to sleep until some other enters the same monitor and calls notify (). The general format is:
                           
                               final void wait()throws InterruptedException

notify(): 

Wakes up the first thread that called wait() on the same object. The general format is:
                                           
                                             void notify()

notifyAll(): 

Wakes up all the thread that called wait() on the same object. The highest priority will run first.
                                        
                                                void notifyAll()

Sample program for interthread communication in java:

class q
{
int n;
boolean valueset=false;
synchronized int get()
{
if(!valueset)
{
try{
wait();
}
catch(InterruptedException e)
{
System.out.println(“Interrupted exception caught”);
}
}
System.out.println(“got:”+n);
valueset=false;
notify();
return n;
}
}
synchronized void put()
{
if(valueset)
{
try{
wait();
}
catch(InterruptedException e)
{
System.out.println(“Interrupted exception caught”);
}
this.n=n;
valueset=true;
System.out.println(“put:”+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q p)
{
this.q=q;
new Thread(this,”Producer”).start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q ;
Consumer(Q q)
{
this.q=q;
new Thread(this,”consumer”).start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class PCFixed
{
public static void main(String arg[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.prinln(“press control - C to stop”);
}
}




Inside get(),wait() is called. This causes its execution to suspend until the producer notifies you that some data is ready. When this happens, execution inside get() resumes. After the data has been obtained, get() calls notify(). This tells the producer that it is ok 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.

Here is some output from this program:
put:1
got:1
put:2
got:2
put:3
got:3
put:4
got:4
put:5
got:5



No comments:

Post a Comment