Java Multi-thread ~ ~ ~ Using exchanger to exchange data between threads [this combination of multi-threaded parallelism will solve a lot of problems]

Source: Internet
Author: User

http://blog.csdn.net/a352193394/article/details/39503857

Java Multi-thread ~ ~ ~ Using exchanger to exchange data between threads [this combination of multi-threaded parallel will solve a lot of problems] specifically see http://www.cnblogs.com/donaldlee2008/p/5290169.html java Thread pool Parallel Execution http://www.cnblogs.com/donaldlee2008/p/5290169.htmlJava Multi-thread ~ ~ ~ Exchange data between threads using exchanger2014-09-23 20:48 1205 people read comments (0) favorite reports Classification:Multithreading (+)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

In multi-threading, exchanging data between two threads is very common, and we can use a common data structure, and Java also provides a good

Class for us to use, that is the exchanger class, this class can help us to synchronize data structure between two threads, and then we will take this class to real

Now a producer consumer model, it seems that the model has been written rotten.

[Java]View PlainCopyprint?
  1. Package com.bird.concursey.charpet5;
  2. Import java.util.List;
  3. Import Java.util.concurrent.Exchanger;
  4. Public class Producer implements Runnable {
  5. //this'll is the data structure that the producer would interchange with the consumer.
  6. private list<string> buffer;
  7. private exchanger<list<string>> Exchanger;
  8. Public Producer (list<string> buffer, exchanger<list<string>> Exchanger) {
  9. super ();
  10. this.buffer = buffer;
  11. This.exchanger = exchanger;
  12. }
  13. @Override
  14. public Void Run () {
  15. int cycle = 1;
  16. For (int i = 0; i < i++) {
  17. System.out.printf ("Producer:cycle%d\n", Cycle);
  18. For (int j=0; j<; j + +) {
  19. String message="Event" + ((i*) +j);
  20. System.out.printf ("Producer:%s\n", message);
  21. Buffer.add (message);
  22. }
  23. try {
  24. Buffer = exchanger.exchange (buffer);
  25. } catch (Interruptedexception e) {
  26. //TODO auto-generated catch block
  27. E.printstacktrace ();
  28. }
  29. System.out.println ("Producer:" +buffer.size ());
  30. cycle++;
  31. }
  32. }
  33. }

[Java]View PlainCopyprint?
  1. Package com.bird.concursey.charpet5;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import Java.util.concurrent.Exchanger;
  5. Public class Consumer implements Runnable {
  6. private list<string> buffer;
  7. private exchanger<list<string>> exchange;
  8. Public Consumer (list<string> buffer, exchanger<list<string>> Exchange) {
  9. super ();
  10. this.buffer = buffer;
  11. this.exchange = Exchange;
  12. }
  13. @Override
  14. public Void Run () {
  15. int cycle = 1;
  16. For (int i = 0; i < i++) {
  17. System.out.printf ("Consumer:cycle%d\n", Cycle);
  18. try {
  19. Buffer = exchange.exchange (buffer);
  20. } catch (Interruptedexception e) {
  21. E.printstacktrace ();
  22. }
  23. System.out.println ("Consumer:" +buffer.size ());
  24. For (int j=0; j<; j + +) {
  25. String Message=buffer.get (0);
  26. System.out.println ("Consumer:" +message);
  27. Buffer.remove (0);
  28. }
  29. cycle++;
  30. }
  31. }
  32. public static void Main (string[] args) {
  33. list<string> buffer1 = new arraylist<string> ();
  34. list<string> buffer2 = new arraylist<string> ();
  35. Exchanger<list<string>> Exchange = new exchanger<list<string>> ();
  36. Producer Producer = new Producer (Buffer1, Exchange);
  37. Consumer Consumer = new Consumer (buffer2, Exchange);
  38. Thread threadproducer=new Thread (producer);
  39. Thread threadconsumer=new Thread (consumer);
  40. Threadproducer.start ();
  41. Threadconsumer.start ();
  42. }
  43. }

[HTML]View PlainCopyprint?
    1. The consumer begins with a empty buffer and calls Exchanger to synchronize with the
    2. Producer. It needs data to consume. The producer begins its execution with an empty buffer.
    3. It creates strings, stores it in the buffer, and uses the exchanger-synchronize with
    4. The consumer.
[HTML]View PlainCopyprint?
    1. At this point, both threads (producer and consumer) is in Exchanger and it changes the
    2. Data structures, if the consumer returns from the Exchange () method, it would have a
    3. Buffer with ten strings. When the producer returns from the Exchange () method, it would have
    4. An empty buffer to fill again. This operation is to be repeated ten times.
[HTML]View PlainCopy print?
    1. If You execute the example, you'll see how producer and consumer do their jobs
    2. Concurrently and how the other objects interchange their buffers in every step. As it occurs with
    3. Other synchronization utilities, the first thread that calls the Exchange () method is put to
    4. Sleep until the other threads arrived.

Java Multi-thread ~ ~ ~ Using exchanger to exchange data between threads [this combination of multi-threaded parallelism will solve a lot of problems]

Related Article

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.