Inter-thread Communication in Java: Synchronizing Threads for Cooperation

Learn about inter-thread communication in Java, which enables synchronized threads to cooperate and communicate with each other. Explore methods provided by the Object class to facilitate thread synchronization and communication effectively in your Java applications.



Inter-thread Communication in Java

Inter-thread communication or cooperation allows synchronized threads to communicate with each other. This is achieved using methods provided by the `Object` class:

  • wait(): Causes the current thread to release the lock and wait until another thread notifies it.
  • notify(): Wakes up a single thread that is waiting on the object's monitor.
  • notifyAll(): Wakes up all threads that are waiting on the object's monitor.

wait() Method

The wait() method causes the current thread to wait until another thread invokes notify() or notifyAll() on the same object.

  • public final void wait() throws InterruptedException: Waits until the object is notified.
  • public final void wait(long timeout) throws InterruptedException: Waits for the specified amount of time.

notify() Method

The notify() method wakes up a single thread that is waiting on this object's monitor.

  • public final void notify(): Wakes up one thread waiting on the object.

notifyAll() Method

The notifyAll() method wakes up all threads that are waiting on this object's monitor.

  • public final void notifyAll(): Wakes up all threads waiting on the object.

Understanding Inter-thread Communication Process

Inter-thread communication involves threads acquiring and releasing locks on objects to synchronize their execution. Here's a basic explanation:

  1. Threads enter to acquire lock.
  2. One thread acquires the lock and enters the critical section.
  3. If another thread calls wait(), it goes into waiting state and releases the lock.
  4. When another thread calls notify() or notifyAll(), the waiting thread moves to a runnable state.
  5. Once the task is completed, the thread releases the lock and exits the monitor state of the object.

Example of Inter-thread Communication in Java

Here's a simple example demonstrating inter-thread communication:

FileName: Test.java

class Customer {    
int amount = 10000;    
    
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 (InterruptedException e) {}    
    }    
    this.amount -= amount;    
    System.out.println("Withdraw completed...");    
}    
    
synchronized void deposit(int amount) {    
    System.out.println("Going to deposit...");    
    this.amount += amount;    
    System.out.println("Deposit completed... ");    
    notify();    
}    
}    
    
class Test {    
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

Going to withdraw...
Less balance; waiting for deposit...
Going to deposit...
Deposit completed... 
Withdraw completed...
    

Why wait(), notify(), and notifyAll() Methods are in Object class?

These methods are defined in the Object class because they are related to object locks and synchronization.

Difference between wait() and sleep() Methods

wait() sleep()
The wait() method releases the lock. The sleep() method does not release the lock.
Method of Object class. Method of Thread class.
Non-static method. Static method.
Should be notified by notify() or notifyAll(). Completes sleep after specified time.