In this article wait(), notify(), and notifyAll() Methods in Java we give the information about these methods help coordinate multiple threads so that one thread can pause its execution until another thread completes a particular task.

wait(), notify(), and notifyAll() Methods in Java

Introduction

In Java, threads can communicate with each other using three important methods:
wait(), notify(), and notifyAll().

These methods help coordinate multiple threads so that one thread can pause its execution until another thread completes a particular task.

  1. What is wait() Method?

The wait() method is used for inter-thread communication in Java.
When a thread calls wait(), it temporarily releases the lock on the object and enters the waiting state until another thread calls notify() or notifyAll() on the same object.

Syntax:

object.wait();

Important Points:

  • wait() is defined in the Object class, not in the Thread class.
  • It must be called inside a synchronized block or synchronized method.
    Otherwise, it throws an IllegalMonitorStateException.
  • When a thread calls wait(), it:
    1. Releases the lock on the object.
    2. Enters the waiting state.
    3. Waits until it is notified using notify() or notifyAll().

How wait() Works

  1. A thread enters a synchronized method or block.
  2. When wait() is called, the thread releases the lock and pauses its execution.
  3. Another thread can now acquire the lock and perform its task.
  4. When the other thread calls notify() or notifyAll() on the same object,
    the waiting thread is awakened and becomes runnable again.
  1. What is notify() Method?

The notify() method wakes up one thread that is waiting on the same object.
Only one thread (chosen by the JVM) will be moved from the waiting state to the runnable state.

Syntax:

object.notify();

Key Point:

The awakened thread does not run immediately — it must reacquire the lock before continuing.

  1. What is notifyAll() Method?

The notifyAll() method wakes up all threads that are waiting on the same object.
After being notified, they compete for the lock, and whichever thread gets it first continues execution.

Syntax:

object.notifyAll();

Example: Producer–Consumer Problem using wait() and notify()

Concept:

  • Producer Thread: Produces data and stores it in a shared object.
  • Consumer Thread: Consumes the data from the shared object.
  • If the producer has already produced data, it should wait until the consumer consumes it.
  • Similarly, the consumer should wait if there is no data produced yet.

Program:

// Shared resource class

class Data

{

private int value;

private boolean flag = false;  // false → no data, true → data available

// Producer method

synchronized void produce(int v)

{

try

{

// If data already exists, wait for consumer

while (flag)

{

wait();

}

value = v;

System.out.println(“Produced: ” + value);

flag = true;

notify();  // Wake up consumer

}

catch (Exception e)

{

System.out.println(e);

}

}

// Consumer method

synchronized void consume()

{

try

{

// If no data, wait for producer

while (!flag)

{

wait();

}

System.out.println(“Consumed: ” + value);

flag = false;

notify();  // Wake up producer

}

catch (Exception e)

{

System.out.println(e);

}

}

}

// Producer thread

class Producer extends Thread

{

Data d;

Producer(Data d)

{

this.d = d;

}

public void run()

{

for (int i = 1; i <= 5; i++)

{

d.produce(i);

try

{

Thread.sleep(500);

}

catch (Exception e)

{}

}

}

}

// Consumer thread

class Consumer extends Thread

{

Data d;

Consumer(Data d)

{

this.d = d;

}

public void run()

{

for (int i = 1; i <= 5; i++)

{

d.consume();

try

{

Thread.sleep(500);

}

catch (Exception e)

{}

}

}

}

// Main class

public class WaitExample

{

public static void main(String[] args)

{

Data d = new Data();

Producer p = new Producer(d);

Consumer c = new Consumer(d);

p.start();

c.start();

}

}

Program Output:

Produced: 1

Consumed: 1

Produced: 2

Consumed: 2

Produced: 3

Consumed: 3

Produced: 4

Consumed: 4

Produced: 5

Consumed: 5

Explanation:

  1. The Producer thread produces one value and sets the flag to true.
  2. The Consumer thread waits until the flag becomes true, then consumes the data.
  3. After consuming, it sets the flag to false and calls notify() to wake up the producer.
  4. This cycle continues until all data is produced and consumed.
  5. Both threads use wait() and notify() for smooth coordination.

Key Points:

  • wait(), notify(), and notifyAll() are methods of Object class, not Thread class.
  • These methods are used only inside synchronized blocks or methods.
  • wait() releases the lock, while notify() and notifyAll() do not release the lock immediately.
  • These methods help achieve thread communication and prevent race conditions.
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Leave a Reply

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