Exchanger in Java

What is an Exchanger ?

 
Exchanger simplifies data exchange between two threads.

Exchanger class provides a point of data interchange between two threads.

An Exchanger waits until two threads call its exchange() method. When this method is invoked, the exchanger exchanges data supplied by two threads.

Java Exchanger
 

Exchanger class

 
Exchanger is a generic class declared as follows :

Exchanger

Here T is the type of data being exchanged.

 
Exchanger class has a single method exchange().

This method synchronizes exchange of data.

Syntax :


exchange(V x) 

exchange(V x, long timeout, TimeUnit unit)

The first form 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.

The second form 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.
 

Exchanger example program

 
Here is a example program that demonstrates using an Exchanger to exchange data between two threads.

package com.topjavatutorial;

import java.util.concurrent.Exchanger;

public class StringExchangerDemo {

  public static void main(String[] args) {
    Exchanger<String> exchanger = new Exchanger<>();
    
    StringProducer stringProducer = new StringProducer(exchanger);
    StringConsumer stringConsumer = new StringConsumer(exchanger);
    
    new Thread(stringProducer).start();
    new Thread(stringConsumer).start();
  }

}


 
The main method in above class creates an Exchanger for two strings.

This Exchanger object then used to synchronize exchange of strings between two threads.
 

package com.topjavatutorial;

import java.util.concurrent.Exchanger;

public class StringProducer implements Runnable {

  Exchanger<String> exchanger;
  String str;

  public StringProducer(Exchanger<String> exchanger) {
    this.exchanger = exchanger;
    str = new String();
  }

  @Override
  public void run() {
    char ch = 'A';
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 5; j++) {
        str += ch++;
      }

      try {
        str = exchanger.exchange(str);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

}


 
The StringProducer class above produces 5 character Strings. The StringConsumer class below creates empty string and exchanges for a full string.
 
package com.topjavatutorial;

import java.util.concurrent.Exchanger;

public class StringConsumer implements Runnable {

  Exchanger<String> exchanger;
  String str;
  
  public StringConsumer(Exchanger<String> exchanger){
    this.exchanger = exchanger;
  }
  
  @Override
  public void run() {
    
    for(int i=0;i<3;i++){
      try {
        str = exchanger.exchange(new String());
        System.out.println("Received String : " + str);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  }

}


 

Output

 
Received String : ABCDE
Received String : FGHIJ
Received String : KLMNO
 

Using Exchanger

 
Exchanger class may be very useful in a situation similar to the producer-consumer problem. In producer-consumer problem where you have a common buffer of data, one or more producers of data, and one or more consumers of data. As the Exchanger class only synchronizes two threads, you can use it if you have a producer-consumer problem with one producer and one consumer.

 

You may also like

CountDownLatch in Java

CyclicBarrier in Java

Top 10 Java Collection articles

 

Reference

 

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags