Inter Thread Communication In Java

Inter-thread communication, also known as cooperation, involves enabling synchronized threads to exchange information with each other.

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class :

  • wait
  • notify
  • notifyAll
  • 1) wait method

The wait method allows the current thread to release the lock and wait until another thread calls the notify or notifyAll method for this object, or until a specific duration of time has passed.

The object's monitor can only be owned by the current thread, requiring it to be invoked from within the synchronized method to prevent exceptions from being thrown.

Method Description
public final void wait()throws InterruptedException It waits until object is notified.
public final void wait(long timeout)throws InterruptedException It waits for the specified amount of time.

2) notify method

The notify function is responsible for awakening a solitary thread that is in a state of waiting on the monitor of this object. When multiple threads are in a waiting state on this object, the system randomly selects one of them to be woken up. The selection process is arbitrary and depends on the specific implementation being used.

Syntax:

Example

public final void notify()

3) notifyAll method

Resumes all threads that are currently in a wait state on the monitor of this object.

Syntax:

Example

public final void notifyAll()

Understanding the process of inter-thread communication

The point to point explanation of the above diagram is as follows:

  • Threads enter to acquire lock.
  • Lock is acquired by on thread.
  • Now thread goes to waiting state if you call wait method on the object. Otherwise it releases the lock and exits.
  • If you call notify or notifyAll method, thread moves to the notified state (runnable state).
  • Now thread is available to acquire lock.
  • After completion of the task, thread releases the lock and exits the monitor state of the object.
  • Why wait, notify and notifyAll methods are defined in Object class not Thread class?

This connection exists due to the fact that locks are associated with objects, and every object possesses a lock.

Difference between wait and sleep?

Let's explore the key distinctions between the wait and sleep methods.

wait() sleep()
The wait() method releases the lock. The sleep() method doesn't release the lock.
It is a method of Object class It is a method of Thread class
It is the non-static method It is the static method
It should be notified by notify() or notifyAll() methods After the specified amount of time, sleep is completed.

Example of Inter Thread Communication in Java

Let's explore a straightforward example showcasing inter-thread communication.

Example

Example

//Java Program to understand the use of Inter-Thread Communication

class Customer{    

 int amount=10000;    

 //creating a withdraw() method which calls the wait() method

 synchronized void withdraw(int amount){    

  System.out.println("going to withdraw...");    

    

  if(this.amount<amount){    

    System.out.println("Less balance; waiting for deposit...");    

    try{wait();}catch(Exception e){}    

  }    

  this.amount-=amount;    

  System.out.println("withdraw completed...");    

 }    

 //creating a deposit() method with calls the notify() method

 synchronized void deposit(int amount){    

  System.out.println("going to deposit...");    

  this.amount+=amount;    

  System.out.println("deposit completed... ");    

  notify();    

 }    

}    

//Creating a Main class to start threads and call deposit() and withdraw()

public class Main{    

 public static void main(String args[]){    

  final Customer c=new Customer();    

  new Thread(){    

    public void run(){c.withdraw(15000);}    

  }.start();    

  new Thread(){    

    public void run(){c.deposit(10000);}    

  }.start();    

 }

}

Output:

Output

going to withdraw...

Less balance; waiting for deposit...

going to deposit...

deposit completed...

withdraw completed

Input Required

This code uses input(). Please provide values below: