How to exchange data between two threads using Exchanger in Java

We will discuss about how to exchange data between two threads or a pair of threads. The Exchanger class under java.util.concurrent package was introduced in JDK 1.5 along with CyclicBarrier, CountDownLatch to exchange data between two threads only.

A synchronization point at which two threads can pair and exchange data within pair using the exchange() method of the Exchanger class. An exchanger waits until both threads call its exchange() method to exchange data. An exchanger may be viewed as a bidirectional form of a Blocking Queue.

java.util.concurrent.Exchanger class has the following methods:

V exchange(V x) – waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return.

V exchange(V x, long timeout, TimeUnit unit) – waits for another thread to arrive at this exchange point (unless the current thread is interrupted or the specified waiting time elapses), and then transfers the given object to it, receiving its object in return.

The exchange() method of the Exchanger class is blocking method. So when a thread calls exchange() method is blocked on a meeting point until another thread arrives.

Exchanger allows threads to pair up and atomically swap data so that either both threads manage to exchange their data or none of them does.

An exchanger may be useful in applications, such as genetic algorithms, pipeline designs, implementation of thread pools and concurrent data structures, such as stack, queue, channels.

Now let’s create an example to understand the implementation of exchanger.

In this Java concurrency tutorial, we will be creating one producer and one consumer thread, which will exchange the data or message using the Exchanger utility class.

You need to create an object of Exchanger like the following, I am using String type, you can use type as per your requirement:

Exchanger<String> exchanger = new Exchanger<>();

When a thread, let’s say, A wants to exchange message or data, it calls exchange() method and it will be blocked until another thread, let’s say, B comes in and transfers its data to thread A or thread A is interrupted or timed out.

Finally when B wants to exchange data then it also calls exchange() method and data or messages are exchanged between threads A and B.

Finally A gets B’s data and B gets A’s data.

Producer

package com.roytuts.java.exchanger.example;

import java.util.concurrent.Exchanger;

public class Producer implements Runnable {

	private String msg;
	private Exchanger<String> exchanger;

	public Producer(String msg, Exchanger<String> exchanger) {
		this.msg = msg;
		this.exchanger = exchanger;
	}

	@Override
	public void run() {
		while (true) {
			try {
				msg = exchanger.exchange(msg);
				System.out.println("Producer got message: " + msg);
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

Consumer

package com.roytuts.java.exchanger.example;

import java.util.concurrent.Exchanger;

public class Consumer implements Runnable {

	private String msg;
	private Exchanger<String> exchanger;

	public Consumer(String msg, Exchanger<String> exchanger) {
		this.msg = msg;
		this.exchanger = exchanger;
	}

	@Override
	public void run() {
		while (true) {
			try {
				msg = exchanger.exchange(msg);
				System.out.println("Consumer got message: " + msg);
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

Exchange Data

package com.roytuts.java.exchanger.example;

import java.util.concurrent.Exchanger;

public class ExchangerApp {

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

		Producer p = new Producer("I am Producer", exchanger);
		Consumer c = new Consumer("I am Consumer", exchanger);

		(new Thread(p)).start();
		(new Thread(c)).start();
	}

}

Executing the above class will give you the following output:

Consumer got message: I am Producer
Producer got message: I am Consumer
Producer got message: I am Producer
Consumer got message: I am Consumer
Consumer got message: I am Producer
Producer got message: I am Consumer
Producer got message: I am Producer
Consumer got message: I am Consumer
Consumer got message: I am Producer
Producer got message: I am Consumer
...

Download Source Code

Thanks for reading.

Leave a Reply

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