Exchanger sibling data information exchange between threads

Source: Internet
Author: User

First, brief

Exchanger can exchange data between two threads, only 2 threads, and he does not support the interchange of data between more than one thread. When thread A calls the Exchange () method of an Exchange object, he falls into a blocking state until thread B also calls the Exchange () method, and then swaps the data in a thread-safe fashion after threads A and b continue to run.

Second, code example

1  Public classExchangertest {2     3      Public Static voidMain (string[] args) {4Exchanger<string> Exchanger =NewExchanger<>();5Executorservice Executorservice = Executors.newfixedthreadpool (2);6String data = "";7Executorservice.execute (NewProcuder (data, exchanger));8Executorservice.execute (NewConsumer (data, exchanger));9     }Ten  One } A  - classProcuderImplementsrunnable{ -      the     PrivateString data; -     PrivateExchanger<string>Exchanger; -      PublicProcuder (String data,exchanger<string>Exchanger) { -          This. data =data; +          This. Exchanger =Exchanger; -     } +  A @Override at      Public voidrun () { -          -         Try { -              for(inti = 0; I < 5; i++) { -data =NewRandom (). Nextint (1000) + ""; -System.out.println ("producer" +i+ "" +data); inThread.Sleep (NewRandom (). Nextint (5) *1000); - exchanger.exchange (data); to             } +}Catch(interruptedexception e) { - e.printstacktrace (); the         } *     } $     Panax Notoginseng } -  the classConsumerImplementsrunnable{ +      A     PrivateString data; the     PrivateExchanger<string>Exchanger; +      PublicConsumer (String data,exchanger<string>Exchanger) { -          This. data =data; $          This. Exchanger =Exchanger; $     } -  - @Override the      Public voidrun () { -         Wuyi         Try { the              for(inti = 0; I < 5; i++) { -data =exchanger.exchange (data); WuThread.Sleep (NewRandom (). Nextint (5) *1000); -System.out.println ("Consumer" +i+ "" +data); About             } $}Catch(interruptedexception e) { - e.printstacktrace (); -         } -     } A      +}
Producer0 782503782367503 151 367  151

Exchanger sibling data information exchange between threads

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.