zoukankan      html  css  js  c++  java
  • 生产者消费者模式的java实现(实现四)

    实现生产者消费者模式,前面已经讲了传统的三种,我试想能不能用其他的同步用具类实现一下呢,我对semaphore比较熟悉,在控制线程顺序的时候非常方便。试着用semaphore来实现一下生产者消费者模式,成功了。

    package com.smikevon.concurrent;
    
    import java.util.Random;
    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    
    /**
     * @description: 生产者消费者模式,采用semaphore(信号量机制进行处理)
     * @date       : 2014年9月18日 下午4:52:09
     */
    public class ProducerConsumer_03 {
    
        static class Producer implements Runnable {
    
            private Drop drop;
            private Semaphore producerSemaphore;
            private Semaphore consumerSemaphore;
            private String name;
    
            public Producer(Drop drop,Semaphore producerSemaphore,Semaphore consumerSemaphore,String name){
                this.drop = drop;
                this.producerSemaphore = producerSemaphore;
                this.consumerSemaphore = consumerSemaphore;
                this.name = name;
            }
    
            public void run() {
                String[] messages = {
                    "我是",
                    "一名程序员",
                    "我很骄傲",
                    "也很自豪",
                    "爱岗敬业",
                    "勤劳奉献",
                    "无怨无悔",
                    "奉献青春",
                    "希望通过学习",
                    "提升",
                    "自己",
                    "done",
                };
                try {
                    for(int i=0;i<messages.length;i++){
                        producerSemaphore.acquire();
                        System.out.format("%s: 放入信息-----------%s %n",name,messages[i]);
                        Thread.sleep(new Random(System.currentTimeMillis()).nextInt(5000));
                        drop.setMessage(messages[i]);
                        consumerSemaphore.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        static class Consumer implements Runnable{
    
            private Drop drop;
            private Semaphore producerSemaphore;
            private Semaphore consumerSemaphore;
            private String name;
    
            public Consumer(Drop drop,Semaphore producerSemaphore,Semaphore consumerSemaphore,String name){
                this.drop = drop;
                this.producerSemaphore = producerSemaphore;
                this.consumerSemaphore = consumerSemaphore;
                this.name = name;
            }
    
            public void run() {
                try {
                    String message = "";
                    do{
                        consumerSemaphore.acquire();
                        message = drop.getMessage();
                        System.out.format("%s: 取出信息-------------%s %n",name,message);
                        Thread.sleep(new Random(System.currentTimeMillis()).nextInt(5000));
                        producerSemaphore.release();
                    }while(!message.equals("done"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        static class Drop{
            String message;
    
            public String getMessage() {
                return message;
            }
    
            public void setMessage(String message) {
                this.message = message;
            }
    
        }
    
        public static void main(String[] args){
            Drop drop = new Drop();
            Semaphore producerSemaphore = new Semaphore(1);
            Semaphore consumerSemaphore = new Semaphore(0);
    
            Executor executor = Executors.newCachedThreadPool();
            executor.execute(new Producer(drop, producerSemaphore, consumerSemaphore, "生产者"));
            executor.execute(new Consumer(drop, producerSemaphore, consumerSemaphore, "消费者"));
    
        }
    
    }
  • 相关阅读:
    合并两个排序的链表
    反转链表
    java网络编程之TCP通讯
    java网络编程之UDP通讯
    Java中的线程同步机制
    阿里研发工程师面试题三个小结
    Android开发的进阶之路
    获取一个字符串中每一个字母出现的次数使用map集合
    Android常见面试题目
    Java垃圾回收
  • 原文地址:https://www.cnblogs.com/seanvon/p/4072027.html
Copyright © 2011-2022 走看看