生产者消费者模式的java实现(实现三)

  Exchanger是java.util.concurrent类库下的一个并发工具。下面是java api文档里对Exchanger的描述。

  A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner’s object on return. An Exchanger may be viewed as a bidirectional form of a SynchronousQueue. Exchangers may be useful in applications such as genetic algorithms and pipeline designs.

  也就是Exchanger必须是伴随一对线程出现。

package com.smikevon.concurrent;

import java.util.Random;
import java.util.concurrent.Exchanger;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @description: 生产者消费者模式使用exchanger来实现
 * @date       : 2014年9月18日 下午4:28:49
 */
public class ProducerConsumer_02 {

    static class Producer implements Runnable {

        private Exchanger<String> exchanger;
        private String name;

        public Producer(Exchanger<String> exchanger,String name){
            this.exchanger = exchanger;
            this.name = name;
        }

        public void run() {
            String[] messages = {
                "我是",
                "一名程序员",
                "我很骄傲",
                "也很自豪",
                "爱岗敬业",
                "勤劳奉献",
                "无怨无悔",
                "奉献青春",
                "希望通过学习",
                "提升",
                "自己",
                "done",
            };
            try {
                for(int i=0;i<messages.length;i++){
                    System.out.format("%s: 放入信息-----------%s %n",name,messages[i]);
                    exchanger.exchange(messages[i]);
                    Thread.sleep(new Random(System.currentTimeMillis()).nextInt(5000));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Consumer implements Runnable{

        private Exchanger<String> exchanger;
        private String name;

        public Consumer(Exchanger<String> exchanger,String name){
            this.exchanger = exchanger;
            this.name = name;
        }

        public void run() {
            try {
                String message = "";
                do{
                    message = exchanger.exchange("");
                    System.out.format("%s: 取出信息-------------%s %n",name,message);
                    Thread.sleep(new Random(System.currentTimeMillis()).nextInt(5000));
                }while(!message.equals("done"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        Executor executor = Executors.newCachedThreadPool();
        Exchanger<String> exchanger = new Exchanger<String>();
        executor.execute(new Producer(exchanger,"生产者"));
        executor.execute(new Consumer(exchanger,"消费者"));
    }

}

 

    原文作者:星火照天明
    原文地址: http://www.cnblogs.com/seanvon/p/4072012.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞