Exchanger is a suite of tools that has been available since jdk1.5 and is typically used to exchange data between two worker threads. In this article I will take a more down-to-the-way to introduce the analysis of this tool class. First, let's look at the narrative in the official API documentation:
A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object in entry to the Exchange method, matches with a partner thread, and receives its partner ' S object on return. An Exchanger is viewed as a bidirectional form of a synchronousqueue. Exchangers May is useful in applications such as genetic algorithms and pipeline designs.
in the above description, there are several points:
- This class provides the external operation is synchronous;
- Use adulthood to exchange data between the threads that appear;
- Can be regarded as two-way synchronization queue;
- It can be used in genetic algorithms, pipeline design and other scenarios.
Construction Method:
Public Exchanger ()
Public V Exchange (v x) throws Interruptedexception
Public v Exchange (v X, long timeout, timeunit unit) throws Interruptedexception, TimeoutException
From the official Javadoc, it is known that when a thread reaches the exchange call point, if its partner thread has previously called this method, its partners are dispatched to wake up and Exchange objects with them, and then return each. If its partner has not yet reached the exchange point, then the front thread will be suspended until the partner thread arrives-the exchange returns normally, or when the front-end is interrupted-throws an interrupt exception, or waits for a timeout-throws a timeout exception.
Examples of Use
The use of exchanger in producer and consumer models is as follows:
Package Concurrent;import Java.util.arraylist;import Java.util.List; Import Java.util.random;import Java.util.concurrent.Exchanger; Public class exchangertest { Public Staticvoid Main (string[] args) {exchanger<List<Integer>> exchanger =NewExchanger<> ();NewConsumer (Exchanger). Start ();NewProducer (Exchanger). Start (); }} class Producer extends Thread { List<Integer>List=NewArraylist<> (); exchanger<List<Integer>> exchanger =NULL; PublicProducer (exchanger<List<Integer>> Exchanger) {super (); This.exchanger = exchanger; } @Override Publicvoid Run () {Random Rand =NewRandom (); for(int i=0; i<Ten; i++) {List. Clear ();List. Add (Rand.nextint (10000));List. Add (Rand.nextint (10000));List. Add (Rand.nextint (10000));List. Add (Rand.nextint (10000));List. Add (Rand.nextint (10000));Try{List= Exchanger.exchange (List); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); } } }} class Consumer extends Thread { List<Integer>List=NewArraylist<> (); exchanger<List<Integer>> exchanger =NULL; PublicConsumer (exchanger<List<Integer>> Exchanger) {super (); This.exchanger = exchanger; } @Override Publicvoid Run () { for(int i=0; i<Ten; i++) {Try{List= Exchanger.exchange (List); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); } System.out.Print(List. Get (0)+", "); System.out.Print(List. Get (1)+", "); System.out.Print(List. Get (2)+", "); System.out.Print(List. Get (3)+", "); System.out.println (List. Get (4)+", "); } }}
Java concurrent Programming-exchanger of synchronous helper classes