Inter-thread Communication in Java
Inter-thread communication in Java allows threads to communicate with each other using methods like wait()
, notify()
, and notifyAll()
. These methods are part of the Object
class and are used to solve synchronization problems by allowing threads to pause execution and be notified by other threads.
Step 1: Understanding wait(), notify(), and notifyAll()
wait()
: Causes the current thread to release the lock and wait until another thread callsnotify()
ornotifyAll()
on the same object.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.
Step 2: Example of Inter-thread Communication
Below is an example demonstrating inter-thread communication where one thread produces data, and another thread consumes it:
class SharedResource { private int data; private boolean hasData = false; public synchronized void produce(int value) { while (hasData) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } data = value; hasData = true; System.out.println("Produced: " + data); notify(); } public synchronized void consume() { while (!hasData) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Consumed: " + data); hasData = false; notify(); } } public class InterThreadCommunicationExample { public static void main(String[] args) { SharedResource resource = new SharedResource(); Thread producer = new Thread(() -> { for (int i = 1; i <= 5; i++) { resource.produce(i); try { Thread.sleep(500); // Simulate some delay } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread consumer = new Thread(() -> { for (int i = 1; i <= 5; i++) { resource.consume(); try { Thread.sleep(500); // Simulate some delay } catch (InterruptedException e) { e.printStackTrace(); } } }); producer.start(); consumer.start(); } }
Step 3: Explanation of the Code
The example above demonstrates the following:
- The
produce()
method waits if data is already produced and not consumed yet. Once it produces data, it notifies the consumer thread. - The
consume()
method waits if there is no data to consume. Once it consumes the data, it notifies the producer thread. - The
synchronized
keyword ensures that only one thread accesses the critical section at a time.
Step 4: Using notifyAll()
If multiple threads are waiting, you can use notifyAll()
to wake up all threads:
public synchronized void produce(int value) { while (hasData) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } data = value; hasData = true; System.out.println("Produced: " + data); notifyAll(); // Notify all waiting threads } public synchronized void consume() { while (!hasData) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Consumed: " + data); hasData = false; notifyAll(); // Notify all waiting threads }
Summary
In this tutorial, you learned:
- What inter-thread communication is
- How to use
wait()
,notify()
, andnotifyAll()
- How to implement producer-consumer logic using inter-thread communication
Inter-thread communication is a powerful feature for synchronizing threads and avoiding busy waiting in Java.