Exchanger

Java concurrency API provides a synchronization utility java.util.concurrent.Exchanger that allows two threads to exchange data.

Exchanger provides a common point where two threads arrive and exchange data between them such that data of first thread goes to the second thread and data of second thread goes to first.

exchanger

Exchanger basically creates a barrier at which both threads meet and exchange data. It should be noted that both threads might call exchange() method at different times. The thread calling exchange() method first will wait for other thread to call exchange() method on the exchanger and then data swap will take place.

It should also be noted that Exchanger typically exchanges data between two threads.

Example

In below example code we create two threads and pass them some data via constructor. When the threads run they call exchange() method on a shared exchanger and swap the data with each other.

The Runnable class below takes an Exchanger and data via constructor. In a real word scenario threads might generate their own data or might want to share the result of some data processing.

The run() method is kept bare bones for the sake of simplicity. It calls exchange() method on the exchanger passing it the data that current thread holds.

package com.bharatonjava.concurrency.exchanger;

import java.util.concurrent.Exchanger;

public class ExchangerRunnable implements Runnable {

	private Exchanger<String> exchanger;
	private String data;

	public ExchangerRunnable(Exchanger<String> exchanger, String data) {
		this.exchanger = exchanger;
		this.data = data;
	}

	@Override
	public void run() {
		
		try {

			String newData = exchanger.exchange(data);
			System.out.println(Thread.currentThread().getName() + 
                     " had data=" + data + " and exchanged it for newData="	+ newData);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

}

In main method we create an Exchanger instance that holds String data. Then we create two threads and pass exchanger instance to both the threads. Threads are also passed Strings as data, that they will exchange with each other.

package com.bharatonjava.concurrency.exchanger;

import java.util.concurrent.Exchanger;

public class ExchangerTest {

	public static void main(String[] args) throws InterruptedException {
		
		Exchanger<String> exchanger = new Exchanger<>();
		
		Thread t1 = new Thread(new ExchangerRunnable(exchanger, "ABC"), "Thread-1");
		Thread t2 = new Thread(new ExchangerRunnable(exchanger, "XYZ"), "Thread-2");
		
		t1.start();
		t2.start();
		
		t1.join();
		t2.join();
		
		System.out.println("Main method finished");
	}
}

Output

Thread-2 had data=XYZ and exchanged it for newData=ABC
Thread-1 had data=ABC and exchanged it for newData=XYZ
Main method finished

More..

Exchanger also has an overloaded version of exchange() method that takes a timeout value. Please read javadoc for more details

exchanger1

Further Reading

Exchanger Javadoc

Advertisements

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: