Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Inter Thread Communication

The communication between synchronized threads is known as Inter-Thread communication.
Communication between threads is nothing but releasing & acquiring object locks with the help of each other.

Synchronized threads use wait()notify() notifyall() methods to communicate with each other.

The wait() Method

Java Programming Language Inter Thread Communication wait Method in Multithreading

Syntax

               public final void wait() throws InterruptedException

               public final void wait(long time) throws InterruptedException

A thread that calls wait() method, releases the monitor (lock) and enters into a waiting state until some other thread with the same monitor (lock) calls notify() or notifyAll() method.

The thread that calls wait(long time) method, releases the monitor (lock) and enters into a waiting state for declared amount of time or until some other thread with the same monitor (lock) calls notify() or notifyAll() method.

The notify() Method

Java Programming Language Inter Thread Communication notify Method in Multithreading

Syntax

                           public final void notify()

A thread that calls notify() method, wakes up a thread that has entered into waiting state by calling wait() method with the same monitor.

The notifyAll() Method

A thread that calls notifyAll() method, wakes up all threads that have entered into waiting state by calling wait() method with the same monitor.

Example using wait() & notify() method

 class Lock 
 {
    boolean lock = true;
	
    public synchronized void set()
    {
        if(lock==true)
        {
            System.out.println("Thread1 calls Thread2");
            System.out.println("Hey! Thread2, I am Thread1 here");
            System.out.println("I am trying to execute but you have acquired"+ 
                                                                        "the lock");
            System.out.println("Please release the lock");
            System.out.println("I am waiting for it");
            System.out.println("------------------------------------------------------");
            try
            {
                wait();  // Thread1 waiting
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        System.out.println("Thread1 executed and set lock as true");
        lock = true;
    }
	
    public synchronized void release()
    {
        System.out.println("Thread2 releasing lock");
        lock = false;
        System.out.println("Thread2 calls Thread1");
        System.out.println("Hey! Thread1, I have released the lock");
        System.out.println("------------------------------------------------------");
        notify(); // Thread2 informing to Thread1
    }
 }

 class Thread1 extends Thread
 {
     Lock l;
	
     public Thread1(Lock l)
     {
        this.l = l;
     }
	
     public void run() 
     {
        l.set();
     }
 }

 class Thread2 extends Thread
 {
     Lock l;
	
     public Thread2(Lock l)
    {
        this.l = l;
    }
  	
    public void run() 
    {
        l.release();;
    }
 }

 class InterThreadCommunication 
 {
    public static void main(String args[])
    {
       Lock l = new Lock();
       Thread1 t1 = new Thread1(l);
       Thread2 t2 = new Thread2(l);
       t1.start();
       t2.start();
    }
 }

Output

Thread1 calls Thread2
Hey! Thread2, I am Thread1 here
I am trying to execute but you have acquired the lock
Please release the lock
I am waiting for it
------------------------------------------------------
Thread2 releasing lock
Thread2 calls Thread1
Hey! Thread1, I have released the lock
------------------------------------------------------
Thread1 executed and set lock as true

                                                                             OR

Thread2 releasing lock
Thread2 calls Thread1
Hey! Thread1, I have released the lock
------------------------------------------------------
Thread1 executed and set lock as true

 

Leave a reply