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:
- Monitor Mechanism: Java provides built-in support for inter-thread communication through the monitor mechanism, which allows threads to wait for a condition to occur.
- wait(), notify(), and notifyAll(): These three methods are defined in the
Object
class and are used to facilitate communication between threads. They allow threads to wait for a certain condition (usingwait()
) or to signal other threads that a condition has changed (usingnotify()
ornotifyAll()
). - Synchronization: To use these methods, a thread must own the object's monitor, which is achieved by synchronizing the block of code where these methods are called.
- Communication Patterns: Inter-thread communication can be used in various patterns, such as producer-consumer, where one thread produces data and another thread consumes it.
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
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.