Exchanger

java.util.concurrent.Exchanger Example

In this example, we shall be demonstrating how we can use the java.util.concurrent.Exchanger Class in Java.

The Exchanger Class provides a sort of rendezvous point for two threads, where the threads can exchange their respective Objects with the other thread.
Whenever a thread arrives at the exchange point, it must wait for the other thread to arrive. When the other pairing thread arrives the two threads proceed to exchange their objects.

The Exchanger Class also provides an overloaded version of the parameterless exchange() method, exchange(V x, long timeout, TimeUnit unit). When the exchange method with time-out is used, the waiting thread waits for the period passed as the argument(long timeout). If the corresponding pairing thread does not arrive at the exchange point in that time, the waiting Thread throws a java.util.concurrent.TimeoutException.

Note
If the specified Time-out for the exchange(V x, long timeout, TimeUnit unit) method is less than or equal to zero, the thread will not wait at all at the exchange point.

Let’s try to use the Exchanger Class with the help of an example :

ExchangerExample.java:

package com.javacodegeeks.examples;

import java.util.concurrent.Exchanger;

public class ExchangerExample
{

		Exchanger exchanger = new Exchanger();

		private class Producer implements Runnable
		{
				private String queue;
				@Override
				public void run()
				{
					try
					{
							 //create tasks & fill the queue
							 //exchange the full queue for a empty queue with Consumer
							 queue = exchanger.exchange("Ready Queue");
							 System.out.println(Thread.currentThread().getName()+" now has "+queue);
					}
					catch (InterruptedException e)
					{
							e.printStackTrace();
					}
				}
		}

		private class Consumer implements Runnable
		{

				private String queue;
				@Override
				public void run()
				{
					try
					{
							//do procesing & empty the queue
							//exchange the empty queue for a full queue with Producer
							queue = exchanger.exchange("Empty Queue");
							System.out.println(Thread.currentThread().getName()+" now has "+queue);
					}
					catch (InterruptedException e)
					{
							e.printStackTrace();
					}
				}
		}

		private void start()
		{
				new Thread(new Producer(),"Producer").start();
				new Thread(new Consumer(),"Consumer").start();
		}

		public static void main(String[] args)
		{
				new ExchangerExample().start();
		}

}
OUPUT : 
Consumer now has Ready Queue
Producer now has Empty Queue

In the above example, we create an Exchanger Object of the type String. The Producer thread produces a “filled queue” and exchanges it with the Consumer thread for an “empty queue”.(The filled and empty queue mentioned here are just dummy string object, for the sake of brevity.). Similarly, we can proceed to exchange any type of object between two threads, merely by changing the type parameter of Exchanger instance.

Tip
For cases where more than one thread produces or more than one thread consumes or both, SynchronousQueue is a good option

Conclusion

Thus we have studied the java.util.concurrent.Exchanger Class and how can we use it for our programmes.

Download
You can download the source code of this example here: ExchangerExample.zip

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button