Inter-Thread Communication in Java

Inter-thread communication is a mechanism that allows threads to communicate with each other and synchronize their actions. This is crucial in scenarios where threads need to cooperate to complete a task effectively, especially when one thread needs to wait for another to complete its execution or when they share resources.

Key Points on Inter-Thread Communication:

Example of Inter-Thread Communication:

This example demonstrates a simple producer-consumer scenario using inter-thread communication:

Code Example

class SharedResource {
    private int data;
    private boolean available = false;

    public synchronized int get() throws InterruptedException {
        while (!available) {
            wait(); // Wait until the data is available
        }
        available = false;
        notify(); // Notify the producer that data has been consumed
        return data;
    }

    public synchronized void put(int value) throws InterruptedException {
        while (available) {
            wait(); // Wait until the data is consumed
        }
        data = value;
        available = true;
        notify(); // Notify the consumer that data is available
    }
}

class Producer extends Thread {
    private final SharedResource resource;

    public Producer(SharedResource resource) {
        this.resource = resource;
    }

    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                System.out.println("Producing: " + i);
                resource.put(i);
                Thread.sleep(100); // Simulate time taken to produce
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer extends Thread {
    private final SharedResource resource;

    public Consumer(SharedResource resource) {
        this.resource = resource;
    }

    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                int value = resource.get();
                System.out.println("Consuming: " + value);
                Thread.sleep(150); // Simulate time taken to consume
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class InterThreadCommunicationExample {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();
        Producer producer = new Producer(resource);
        Consumer consumer = new Consumer(resource);

        producer.start();
        consumer.start();
    }
}

Output:

Producing: 0
Consuming: 0
Producing: 1
Consuming: 1
Producing: 2
Consuming: 2
Producing: 3
Consuming: 3
Producing: 4
Consuming: 4

Conclusion:

Inter-thread communication is a powerful feature in Java that allows threads to synchronize their actions and share resources safely. Understanding how to use wait(), notify(), and notifyAll() is essential for developing multi-threaded applications that operate efficiently and without deadlock.