Monday, June 26, 2023

How to use Exchanger for Inter thread communication in Java? Example Tutorial

In Java, you can use the Exchanger class from the java.util.concurrent package to facilitate communication between two threads. The Exchanger provides a synchronization point where two threads can exchange objects. Each thread waits at the exchange() method until both threads have reached it, and then they swap their objects. 

 Here's an example that demonstrates how to use the Exchanger class for inter-thread communication:


import java.util.concurrent.Exchanger;

class FirstThread extends Thread {
    private Exchanger exchanger;

    public FirstThread(Exchanger exchanger) {
        this.exchanger = exchanger;
    }

    public void run() {
        try {
            // Sending a message to the second thread
            String message = "Hello from the first thread!";
            System.out.println("First thread sends: " + message);
            exchanger.exchange(message);

            // Receiving a message from the second thread
            String receivedMessage = exchanger.exchange(null);
            System.out.println("First thread receives: " + receivedMessage);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class SecondThread extends Thread {
    private Exchanger exchanger;

    public SecondThread(Exchanger exchanger) {
        this.exchanger = exchanger;
    }

    public void run() {
        try {
            // Receiving a message from the first thread
            String receivedMessage = exchanger.exchange(null);
            System.out.println("Second thread receives: " + receivedMessage);

            // Sending a message to the first thread
            String message = "Hello from the second thread!";
            System.out.println("Second thread sends: " + message);
            exchanger.exchange(message);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class ExchangerExample {
    public static void main(String[] args) {
        Exchanger exchanger = new Exchanger<>();

        FirstThread firstThread = new FirstThread(exchanger);
        SecondThread secondThread = new SecondThread(exchanger);

        firstThread.start();
        secondThread.start();

        try {
            firstThread.join();
            secondThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

In this example, we have two threads: FirstThread and SecondThread. Both threads share a common Exchanger object. 

The FirstThread sends a message to the SecondThread by calling exchanger.exchange(message), where message is the object to be exchanged. The thread then waits until the SecondThread also reaches the exchange() method. Once both threads have reached the exchange point, they swap their objects. 

Similarly, the SecondThread receives the message sent by the FirstThread by calling exchanger.exchange(null), and then sends its own message by calling exchanger.exchange(message). 

When you run this example, the output might look like this:


First thread sends: Hello from the first thread!
Second thread receives: Hello from the first thread!
Second thread sends: Hello from the second thread!
First thread receives: Hello from the second thread!

As you can see, the messages are exchanged between the two threads using the Exchanger, allowing them to communicate with each other.

No comments:

Post a Comment