Inter-thread communication in Java

In this tutorial, we are going to learn about Inter-thread communication in Java.

Allowing multiple synchronized threads to communicate with each other and maintaining one thread object execution at a time(with multiple thread objects in a row) is known as Inter-thread communication in Java. This is also known as Thread-safe operation or Thread synchronization.

The synchronized keyword implicitly uses three methods for Inter-thread communication. All the classes can acquire these methods because they belong to Object class. They all are declared as final and must be used in a synchronized block only. These are the following methods:

  1. public void wait(): This method is used to send a current thread object into a waiting state till another thread invokes the notify() method or notifyAll() method for this object.
  2. public void notify(): This method is used to send a single thread object which is in waiting state into a running state.
  3. public void notifyAll(): This method is used to send multiple thread objects that are in a waiting state into a running state.

Program:

Let’s see a program of inter-thread. Below is the Java code :

class MobileRecharge {
    int wallet = 400;
    public MobileRecharge()
    {
        Thread t1 = new Thread() {
            public void run() {
                // recharging mobile
                rechargeOf(500);
            }
        };
        t1.start();

        Thread t2=new Thread() {
            public void run() {
                // adding money into wallet
                addingMoneyInWallet(1000);
            }
        };
        t2.start();
    }
    // method to re-charge mobile
    public synchronized void rechargeOf(int amount) {
        // if wallet money is less, then it will wait for adding money in wallet
        if (wallet < amount) {
            System.out.println("less money in wallet, waiting for addition of money...");
            try {
                wait();
            } catch (Exception e) {
            }
        }
        // if money is sufficient then recharge will be done & amount will be debited
        // from wallet
        wallet -= amount;
        System.out.println("Recharge is successful...!!!");
        System.out.println("Remaining money in wallet is : " + wallet);
        notify();
    }

    // method for adding money into the wallet
    public synchronized void addingMoneyInWallet(int amount) {
        if (wallet > amount) {
            try {
                wait();
            } catch (Exception e) {
            }
        }
        wallet += amount;
        System.out.println("Money added in wallet...");
        notify();
    }

}

// main class
class ThreadExample extends Thread{
    public static void main(String[] args) {
        new MobileRecharge();
    }

}

Output :

The output of the above program will be :

less money in wallet, waiting for addition of money...
Money added in wallet...
Recharge is successful...!!!
Remaining money in wallet is : 900

One response to “Inter-thread communication in Java”

  1. Kishan Mistry says:

    System.out.println(“Recharge is successful…!!!”)
    // in above line semi colon is missing

Leave a Reply

Your email address will not be published. Required fields are marked *