Inter-Thread communication in Java

There are three ways for the Threads to communicate with each other.

 

In the FIRST way, all the threads share the same memory space. If the thread share the same object, then these threads share access to that object’s data member and thus communicate with each other.

In the SECOND way, threads communicate by using thread control methods. The second way includes the following three methods:

       1)suspend()---can suspend itself and wait till another thread resumes it.

       2)resume()---a thread can wake up another waiting thread(run concurrently)

       3)join()---the caller thread can wait for the completion of the called thread.

In the THIRD way, thread communicate by using these 3 methods:

       1)wait()---the calling thread wait until another calls the same thread .

       2)notify()---wakes only the first waiting thread on the same object.

       3)notifyAll()---wakes up all the threads that have been called by wait() on the same object.

Example.......

class sort              //class sort is created

   {

int num=0;          //assigning value 0

Boolean value=false;      //variable value is assigned false

synchronized void get(int num)       //synchronized block is declared

   {

if(value==true)                

try

{

wait();        //thread is made to wait

}

catch(InterruptedException e)

{

System.out.println("Interrupted Exception find");

}

this.num=num;         //num value point to the current class object

System.out.println("consumee:"+num);

value=false;

notify();                //notify method is invoked

}

synchronized void put(int num)

   {

if(value==true)

try

{

wait();                   //thread is made to wait

}

                catch(InterruptedException e)

  {

System.out.println("Interrupted Exception find");

}

this.num=num;

System.out.println("Producee:"+num);

value=false;

notify();

          }

      }

//class construct is declared using keyword extends

class construct extends Thread

   {

sort s;             //sort is declared with object s(synchronized method)

construct(sort s)

   {

this.s=s;            //s points to the current class objects

this.start();

   }

public void run()

   {

int i=0;

s.put(++i);           //increments the value num

   }

      }

//class user is declared using extends keyword

class user extends Thread

    {

sort s;

user(sort s)           //constructor is called with sort s(synchronized method)

    {

this.s=s;               //s points to the current class objects

this.start();

    }

public void run()

    {

int i=0;

s.get(++i);

    }

        }

public class interrThread

    {

public static void main(String aa[])

    {

sort s=new sort();

construct ob=new construct(s);

user ob2=new user(s);

    }

       }

Explanation…

  In this program two threads construct and user share the synchronized methods of the class sort. At the time of program execution, the put() of the construct class is invoked, which increments the variable num by 1. After producing 1 by the construct, the get() is invoked by the user class.

Inter thread communication