Advertisement



< Prev
Next >



Interthread Communication



In order to have a smooth interthread communication, we can use three methods of Object class, which are inherited by all the java classes- -:


Note


Calling of wait(), notify(), notifyAll() method is only possible from within the synchronized context i.e. from within a synchronized method or a synchronized block.





How wait(),notify() method help an interthread communcation?







No interthread communication without the use of wait() and notify()


Without the use of wait() and notify() methods, when both threads start at the same time, Thread2 may print the table of 6 before Thread1 has finished printing the table of 5
class B implements Runnable
{ 
public void run()
{
synchronized(this)  //Synchronizing the object of B, this refers to ob
{
	System.out.println("Thread2 running and printing table of 5");
	for(int i=1; i<=10; i++)
	{
		System.out.println("5 x "+ i + " = " + (5*i));	  
	}
	    
}	//synchronized block ends
}	
}



class A 
{
public static void main(String...ar) 
{
B ob = new B();
Thread t= new Thread(ob , "newThread");     //Calling Thread's constructor & passing the object
					    //of B class that  implemented  Runnable interface
					    //& the name of new thread.
t.start();
 
synchronized(ob)  //Synchronizing the object of B, ob
{               
	System.out.println("main thread running and printing table of 6");
	for(int i=1; i<=10; i++)
	{
		System.out.println("6 x "+ i + " = " + (6*i));	  
	}	  
}	//synchronized block ends
    
}	
}

Output -
main thread running and printing table of 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
newThread running and printing table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50



Advertisement




Displaying interthread communication using wait() and notify()


  • The new thread will print a table of 5 and the main thread will print a table of 6. We will use wait() and notify() methods for communcation between these two threads, in such a way that table of 5 is printed before table 6, to maintain an ascending order.
  • class B implements Runnable
    { 
    public void run()
    {
    
    synchronized(this)  //Synchronizing the object of B, this refers to ob
    {
    	System.out.println("newThread running and printing table of 5");
    	for(int i=1; i<=10; i++)
    	{
    		System.out.println("5 x "+ i + " = " + (5*i));	  
    	}
    	    
    	notify();	//notifying the main thread to continue its work    
    
    }	//synchronized block ends
    }	
    }
    
    
    
    class A 
    {
    public static void main(String...ar) 
    {
    B ob = new B();
    Thread t= new Thread(ob , "newThread");     //Calling Thread's constructor & passing the object
    					    //of B class that  implemented  Runnable interface
    					    //& the name of new thread.
    t.start();
     
    synchronized(ob)  //Synchronizing the object of B, ob
    {         
    try
    {
            
            ob.wait();	//main thread stops its execution and waits until it is notify()
    	System.out.println("main thread running and printing table of 6");
    	for(int i=1; i<=10; i++)
    	{
    		System.out.println("6 x "+ i + " = " + (6*i));	  
    	}	  
    }
    catch(InterruptedException e)
    {
    	System.out.println(e);
    }
    }	//synchronized block ends
        
    }	
    }
    
    

    Output is - :

    newThread running and printing table of 5
    5 x 1 = 5
    5 x 2 = 10
    5 x 3 = 15
    5 x 4 = 20
    5 x 5 = 25
    5 x 6 = 30
    5 x 7 = 35
    5 x 8 = 40
    5 x 9 = 45
    5 x 10 = 50
    main thread running and printing table of 6
    6 x 1 = 6
    6 x 2 = 12
    6 x 3 = 18
    6 x 4 = 24
    6 x 5 = 30
    6 x 6 = 36
    6 x 7 = 42
    6 x 8 = 48
    6 x 9 = 54
    6 x 10 = 60


    In the example above, the method call to wait() or notify() method has been made from within a synchronized context, otherwise such smooth interthread coummunication would not have been possible.


    Please share this article -





    < Prev
    Next >
    < Synchronized multiple threads
    Deadlock in Threads >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement