This is the 26th day of my participation in the Gwen Challenge.More article challenges


preface

Java provides cyclicbarriers and countdownlatches in java.util.Concurrent. If you are interested, take a look at the portal: Today we will talk about the next tool in the java.util.concurrent tool class, which is non-toxic, and the use of CountDownLatch. Sano1100a is a Multi-threaded interactive tool provided by Java for communication and data exchange between threads.

Exchanger

Introduction:

Sano1100is an encapsulation tool provided in JDK 1.5 for data exchange between two worker threads. Simply put, when a thread wants to exchange data with another thread after completing a transaction, the first thread to retrieve data will wait for the second thread. The data is not exchanged until the second thread arrives with the data.

By definition, the SERVER is used for communication and data exchange between threads. The Exchange method is used for synchronization. When two threads invoke the Exchange method, the two threads wait for each other until the thread reaches the exchange call point, regardless of the time sequence, and the two threads exchange data and transfer the data generated by the thread to the other side.

public V exchange(V x) throws InterruptedException
public V exchange(V x, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
Copy the code
  • V exchange(V v): Waits for another thread to reach this exchange point (unless the current thread is interrupted), then passes the given object to that thread and receives the object from that thread.
  • V exchange(V v, long timeout, TimeUnit unit): Waits for another thread to reach the exchange point, or the current thread is interrupted — throws an interrupt exception; Or wait timeouts — throw a timeout exception, pass the given object to the thread, and receive the thread’s object.

Usage Scenarios:

It can be applied to genetic algorithm, assembly line design and other scenarios. Genetic algorithms are actually interesting to explain, that is, to test the combination of the genes of two individuals, male A and female B, which will result in the birth of A child.

Is it a compound recovery scenario? Ha ha ha ha!

Ok! To get back to business, today we take a common example in our daily work for the recovery recovery scenario. For example, a common scenario in daily work, “Find different”, and whether there is any difference between the two groups of data comparison.

Code examples:

/** * TODO ** @author Taoze * @version 1.0 * @date 6/25/21 4:06pm */ Public Class ExchangerTest {private static final  Exchanger<String> exchanger = new Exchanger<String>(); private static ExecutorService threadPool = Executors.newFixedThreadPool(2); public static void main(String[] args) { threadPool.execute(new Runnable() { @Override public void run() { try { String A = "12379871924sfkhfksdhfks"; exchanger.exchange(A); } catch (InterruptedException e) { } } }); threadPool.execute(new Runnable() { @Override public void run() { try { String B = "32423423jknjkfsbfj"; String A = exchanger.exchange("B"); System.out.println("A and B are identical: "+ a. quals(B)); System.out.println("A= "+A); System.out.println("B= "+B); } catch (InterruptedException e) { } } }); threadPool.shutdown(); }}Copy the code

Running results:

This is just a simple comparison implementation, we have similar business scenarios can be connected to their own business, such as comparison of two Excel content, etc.;


Ok! That’s all for today’s article, and the above is a simple use method which is SANo1100You can modify it according to your own business. I hope it will be helpful for you. If there is anything wrong, please mention it and we will grow together.

Neat makes for great code, and there’s only so much detail