Java programming thread synchronization tool Exchanger instance parsing, javaexchanger
This article focuses on the use of Java programming thread synchronization tool Exchanger. Let's take a look at the specific content.
If two threads need to exchange information about each other during the running process, for example, a data or space, the Exchanger class is required. Exchanger provides a very convenient way for the thread to exchange information, it can be used as a synchronization point for two threads to exchange objects, only when each thread is inexchange ()
To accept the objects returned by other threads.
Only two threads can exchange data at a time. If there are multiple threads, only two can exchange data. Let's take a look at a common example: hand in one hand for one delivery!
Public class ExchangerTest {public static void main (String [] args) {ExecutorService service = Executors. newCachedThreadPool (); final Exchanger exchanger = new Exchanger (); // defines an exchange object for data exchange // starts a thread execution task service.exe cute (new Runnable () {@ Override public void run () {try {String data1 = "heroin"; System. out. println ("Thread" + Thread. currentThread (). getName () + "bringing out drugs" + data1 + "); Thread. sleep (long) (Math. rando M () * 10000); // transmits the data to be exchanged to the exchange method, and is blocked, waiting for another thread to exchange with it. Returns the exchanged data String data2 = (String) exchanger. exchange (data1); System. out. println ("Thread" + Thread. currentThread (). getName () + "" + data2);} catch (Exception e) {} finally {service. shutdown (); System. out. println ("the transaction is complete. Run with the money! ") ;}}); // Start another thread to execute the task service.exe cute (new Runnable () {@ Override public void run () {try {String data1 =" 3 million "; System. out. println ("Thread" + Thread. currentThread (). getName () + "taking" + data1 + "out"); Thread. sleep (long) (Math. random () * 10000); String data2 = (String) exchanger. exchange (data1); System. out. println ("Thread" + Thread. currentThread (). getName () + "Get with 3 million" + data2);} catch (Exception e) {} finally {service. Shutdown (); System. out. println ("transaction finished. Run with heroin! ");}}});}}
From the code, I seem to have seen two people dealing with drugs ...... Let's take a look at the transaction result:
Thread pool-1-thread-1 is taking out heroin
Thread pool-1-thread-2 is taking out 3 million
Thread pool-1-thread-2 get heroin with 3 million
Thread pool-1-thread-1 exchanged 3 million with heroin
The transaction is complete. Run with heroin!
The transaction is complete. Run with money!
It runs fast. According to the running results, data exchange is indeed realized. This is just to exchange a basic type of data. Its real use is not limited to this, for example, we can exchange an object, which is useful. JDK officially mentioned an advanced application:
The Exchanger is used to swap the buffer between threads. Therefore, when necessary, the thread that fills the buffer zone obtains a new vacant buffer zone and passes the filled buffer zone to the thread that vacated the buffer zone.
This is based on the actual situation. In the same way as above, we must define a buffer class, and then the two threads exchange the buffer class, as for how the class is implemented, it depends on the actual situation. Let's sum up the use of Exchanger ~
Summary
The above is all the content about the instance parsing of the Java programming thread synchronization tool Exchanger. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!