Java Exchanger in Java

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization, Java Exchanger, Java Concurrent Collection

Exchanger class is used to exchange data between two threads. It does that using exchange() method. Exchanger is a generic class that is declared as shown here:

                                public class Exchanger<V>
                              

Here V is the type of data to be exchanged.

Methods

                                exchange(V x)
                                exchange(V x, long timeout, TimeUnit unit)
                              

First form wait for another thread to reach this exchange point and transfer the data to it. Second form waits for another thread (unless timeout waiting time has elapsed or any exception occurred) to reach this exchange point and transfer the data to it.

Below simple Java program demonstrate the use of Exchanger:

JavaExchangerExample.java

                            package com.tutorial.javabasic;

                            import java.util.HashMap;
                            import java.util.concurrent.Exchanger;

                            class Thread1 implements Runnable {

                                private Exchanger<HashMap> exchanger;

                                Thread1(Exchanger<HashMap> latch) {
                                    exchanger = latch;
                                    new Thread(this).start();
                                }

                                @Override
                                public void run() {
                                    for (int i = 1; i <= 4; i++) {
                                        HashMap hm = new HashMap();
                                        hm.put("d" + i, "Data-" + i);
                                        try {
                                            System.out.println("Sending: " + hm);
                                            exchanger.exchange(hm);
                                        } catch (InterruptedException ex) {
                                            ex.printStackTrace();
                                        }
                                    }

                                }
                            }

                            class Thread2 implements Runnable {

                                private Exchanger<HashMap> exchanger;

                                Thread2(Exchanger<HashMap> latch) {
                                    exchanger = latch;
                                    new Thread(this).start();
                                }

                                @Override
                                public void run() {
                                    for (int i = 1; i <= 4; i++) {
                                        HashMap hm = new HashMap();
                                        try {
                                            HashMap receivedData = exchanger.exchange(hm);
                                            System.out.println("Receiving: " + receivedData);
                                        } catch (InterruptedException ex) {
                                            ex.printStackTrace();
                                        }
                                    }

                                }
                            }

                            public class JavaExchangerExample {

                                public static void main(String s[]) {
                                    Exchanger<HashMap> ex = new Exchanger<>();
                                    new Thread1(ex);
                                    new Thread2(ex);
                                }
                            }
                        

Output:

                            Sending: {d1=Data-1}
                            Sending: {d2=Data-2}
                            Receiving: {d1=Data-1}
                            Receiving: {d2=Data-2}
                            Sending: {d3=Data-3}
                            Sending: {d4=Data-4}
                            Receiving: {d3=Data-3}
                            Receiving: {d4=Data-4}
                          

Thread1 is putting data to Exchanger and on other end Thread2 is receiving the same data using the same Exchanger instance.