码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-03 20:40:00      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   使用   java   for   

  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,"消费者"));
    }

}

 

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

标签:des   style   blog   io   color   ar   使用   java   for   

原文地址:http://www.cnblogs.com/seanvon/p/4072012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!