Java.util.concurrent.Exchanger class with Examples Last Updated : 30 May, 2019 Comments Improve Suggest changes Like Article Like Report Exchanger is the most interesting synchronization class of Java. It facilitates the exchange of elements between a pair of threads by creating a synchronization point. It simplifies the exchange of data between two threads. Its operation is simple: it simply waits until two separate threads call its exchange() method. When that occurs, it exchanges the data supplied by the threads. It can also be viewed as a bidirectional SynchronousQueue. It is a generic class that is declared as below. Class Syntax: Exchanger<V> Here, V specifies the type of data being exchanged. Class Hierarchy java.lang.Object ↳ java.util.concurrent.Exchanger<V> Constructor: Exchanger() - Creates a new Exchanger object with default values for its members. Methods: exchange(V x)- When invoked this function causes the current thread to suspend its execution and wait for another thread to call its exchange method. When another thread calls its exchange method, the threads exchange their data and the execution resumes. Syntax: public V exchange(V x) throws InterruptedException exchange(V x, long timeout, TimeUnit unit)- When invoked this function causes the current thread to suspend its execution and wait for another thread to call its exchange method. When another thread calls its exchange method, the threads exchange their data and the execution resumes. The thread waits only for the duration specified by the timeout argument and in case if timeout duration elapses, a TimeoutException is thrown. Syntax: public V exchange(V x, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException Example to demonstrate working of Exchanger class: Java import java.util.concurrent.Exchanger; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class ExchangerDemo { public static void main(String[] args) { Exchanger<String> exchanger = new Exchanger<>(); new UseString(exchanger); new MakeString(exchanger); } } // A thread that makes a string class MakeString implements Runnable { Exchanger<String> ex; String str; MakeString(Exchanger<String> ex) { this.ex = ex; str = new String(); new Thread(this).start(); } public void run() { char ch = 'A'; try { for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { str += ch++; } if (i == 2) { // Exchange the buffer and // only wait for 250 milliseconds str = ex.exchange(str, 250, TimeUnit.MILLISECONDS); continue; } // Exchange a full buffer for an empty one str = ex.exchange(str); } } catch (InterruptedException e) { System.out.println(e); } catch (TimeoutException t) { System.out.println("Timeout Occurred"); } } } // A thread that uses a string class UseString implements Runnable { Exchanger<String> ex; String str; UseString(Exchanger<String> ex) { this.ex = ex; new Thread(this).start(); } public void run() { try { for (int i = 0; i < 3; i++) { if (i == 2) { // Thread sleeps for 500 milliseconds // causing timeout Thread.sleep(500); continue; } // Exchange an empty buffer for a full one str = ex.exchange(new String()); System.out.println("Got: " + str); } } catch (InterruptedException e) { System.out.println(e); } } } Output: Got: ABCDE Got: FGHIJ Timeout Occurred Reference:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Exchanger.html Comment More infoAdvertise with us Next Article Java.util.concurrent.Exchanger class with Examples C CharchitKapoor Follow Improve Article Tags : Java Java - util package Java-concurrent-package Practice Tags : Java Similar Reads Java.util.concurrent.Executor interface with Examples The concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface. It refers to the objects that execute 1 min read Java.util.concurrent.Phaser class in Java with Examples Phaser's primary purpose is to enable synchronization of threads that represent one or more phases of activity. It lets us define a synchronization object that waits until a specific phase has been completed. It then advances to the next phase until that phase concludes. It can also be used to synch 7 min read Java.util.concurrent.ExecutorService Interface with Examples The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads that determine the shutdown status. The ExecutorSer 3 min read ConcurrentSkipListMap in Java with Examples The ConcurrentSkipListMap class is a member of the Java Collections Framework. It was introduced in JDK 1.6, it belongs to java.util.concurrent package. The ConcurrentSkipListMap is a scalable implementation of ConcurrentNavigableMap. All the elements are sorted based on natural ordering or by the C 10 min read ConcurrentSkipListMap put() method in Java with Examples The put() method of java.util.concurrent.ConcurrentSkipListMap is an in-built function in Java which associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced. Syntax: public V put(K key, V value) Parameter: Th 2 min read ConcurrentLinkedDeque pop() method in Java with Examples The Java.util.ConcurrentLinkedDeque.pop() method in Java is used to pop an element from the ConcurrentLinkedDeque. The element is popped from the top of the ConcurrentLinkedDeque and is removed from the same.Syntax: ConcurrentLinkedDeque.pop() Parameters: The method does not take any parameters.Retu 2 min read ConcurrentLinkedDeque push() method in Java with Examples The push() method of ConcurrentLinkedDeque class is an in-built function in Java which pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success an 2 min read ConcurrentLinkedDeque poll() method in Java with Example The poll() method of ConcurrentLinkedDeque returns the front element in the Deque container and deletes it. It returns null if the container is empty. Syntax: public E poll() Parameters: This method does not accept any parameters. Returns: This method returns front element of the Deque container if 2 min read ConcurrentLinkedDeque iterator() method in Java with Example The Java.util.concurrent.ConcurrentLinkedDeque.iterator() method is used to return an iterator of the same elements as that of the ConcurrentLinkedDeque. The elements are returned in random order from what was present in the deque. Syntax: Iterator iterate_value = ConcurrentLinkedDeque.iterator(); P 2 min read ConcurrentLinkedDeque offer() method in Java with Examples The java.util.concurrent.ConcurrentLinkedDeque.offer() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, to the deque. Syntax: public boolean offer(E elem) Parameters: The method accepts a parameter elem which species the element to be inserted to the de 2 min read Like