Java programming thread synchronization tool Exchanger instance parsing, javaexchanger

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.