Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型

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

 

 Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]
具体看 http://www.cnblogs.com/donaldlee2008/p/5290169.html  

java 线程池 并行 执行   http://www.cnblogs.com/donaldlee2008/p/5290169.html  

Java多线程之~~~使用Exchanger在线程之间交换数据

 分类:
 

在多线程中,两个线程之间交换数据是非常常见的情况,我们可以使用公共的数据结构,同样,Java也提供了很好

的类供我们使用,那就是Exchanger类,这个类可以帮助我们在两个线程之间同步数据结构,下面我们以这个类再来实

现一遍生产者消费者模型,貌似这个模型已经被写烂了。

 

 

[java] view plain copy
 
 print?
  1. package com.bird.concursey.charpet5;  
  2.   
  3. import java.util.List;  
  4. import java.util.concurrent.Exchanger;  
  5.   
  6. public class Producer implements Runnable {  
  7.     //This will be the data structure that the producer will interchange with the consumer.  
  8.     private List<String> buffer;  
  9.       
  10.     private Exchanger<List<String>> exchanger;  
  11.       
  12.     public Producer(List<String> buffer, Exchanger<List<String>> exchanger) {  
  13.         super();  
  14.         this.buffer = buffer;  
  15.         this.exchanger = exchanger;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void run() {  
  20.         int cycle = 1;  
  21.         for(int i = 0; i < 10; i++) {  
  22.             System.out.printf("Producer: Cycle %d\n",cycle);  
  23.             for (int j=0; j<10; j++){  
  24.                 String message="Event "+((i*10)+j);  
  25.                 System.out.printf("Producer: %s\n",message);  
  26.                 buffer.add(message);  
  27.             }  
  28.             try {  
  29.                 buffer = exchanger.exchange(buffer);  
  30.             } catch (InterruptedException e) {  
  31.                 // TODO Auto-generated catch block  
  32.                 e.printStackTrace();  
  33.             }  
  34.             System.out.println("Producer: "+buffer.size());  
  35.             cycle++;  
  36.         }  
  37.     }  
  38.   
  39. }  

[java] view plain copy
 
 print?
  1. package com.bird.concursey.charpet5;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.concurrent.Exchanger;  
  6.   
  7. public class Consumer implements Runnable {  
  8.       
  9.     private List<String> buffer;  
  10.       
  11.     private Exchanger<List<String>> exchange;  
  12.       
  13.     public Consumer(List<String> buffer, Exchanger<List<String>> exchange) {  
  14.         super();  
  15.         this.buffer = buffer;  
  16.         this.exchange = exchange;  
  17.     }  
  18.   
  19.   
  20.     @Override  
  21.     public void run() {  
  22.         int cycle = 1;  
  23.         for(int i = 0; i < 10; i++) {  
  24.             System.out.printf("Consumer: Cycle %d\n",cycle);  
  25.             try {  
  26.                 buffer = exchange.exchange(buffer);  
  27.             } catch (InterruptedException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.             System.out.println("Consumer: "+buffer.size());  
  31.             for (int j=0; j<10; j++){  
  32.                 String message=buffer.get(0);  
  33.                 System.out.println("Consumer: "+message);  
  34.                 buffer.remove(0);  
  35.             }  
  36.             cycle++;  
  37.         }  
  38.     }  
  39.       
  40.     public static void main(String[] args) {  
  41.         List<String> buffer1 = new ArrayList<String>();  
  42.         List<String> buffer2 = new ArrayList<String>();  
  43.           
  44.         Exchanger<List<String>> exchange = new Exchanger<List<String>>();  
  45.           
  46.         Producer producer = new Producer(buffer1, exchange);  
  47.         Consumer consumer = new Consumer(buffer2, exchange);  
  48.           
  49.         Thread threadProducer=new Thread(producer);  
  50.         Thread threadConsumer=new Thread(consumer);  
  51.           
  52.         threadProducer.start();  
  53.         threadConsumer.start();  
  54.     }  
  55.   
  56. }  

[html] view plain copy
 
 print?
  1. The consumer begins with an 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 10 strings, stores it in the buffer, and uses the exchanger to synchronize with  
  4. the consumer.  
[html] view plain copy
 
 print?
  1. At this point, both threads (producer and consumer) are in Exchanger and it changes the  
  2. data structures, so when the consumer returns from the exchange() method, it will have a  
  3. buffer with 10 strings. When the producer returns from the exchange() method, it will have  
  4. an empty buffer to fill again. This operation will be repeated 10 times.  
[html] view plain copy
 
 print?
  1. If you execute the example, you will see how producer and consumer do their jobs  
  2. concurrently and how the two objects interchange their buffers in every step. As it occurs with  
  3. other synchronization utilities, the first thread that calls the exchange() method was put to  
  4. sleep until the other threads arrived.  
posted @ 2016-03-19 06:07  donaldlee  阅读(231)  评论(0编辑  收藏  举报