zoukankan      html  css  js  c++  java
  • Semaphore实现的生产者消费者程序

    Semaphore:Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource.

    import java.util.LinkedList;
    import java.util.List;
    import java.util.Random;
    import java.util.concurrent.Semaphore;
    
    class Buffer {
    
        List<Integer> buffer = new LinkedList<Integer>();
        // 互斥量,控制buffer的互斥访问
        private Semaphore mutex = new Semaphore(1);
    
        // canProduceCount可以生产的数量(表示缓冲区可用的数量)。 通过生产者调用acquire,减少permit数目
        private Semaphore canProduceCount = new Semaphore(10);
    
        // canConsumerCount可以消费的数量。通过生产者调用release,增加permit数目
        private Semaphore canConsumerCount = new Semaphore(0);
        Random rn = new Random(10);
    
        public void get() throws InterruptedException {
            canConsumerCount.acquire();
            try {
                mutex.acquire();
                int val = buffer.remove(0);
                System.out
                        .println(Thread.currentThread().getName() + " 正在消费数据为:" + val + "    buffer目前大小为:" + buffer.size());
            } finally {
                mutex.release();
                canProduceCount.release();
            }
    
        }
    
        public void put() throws InterruptedException {
            canProduceCount.acquire();
            try {
                mutex.acquire();
                int val = rn.nextInt(10);
                buffer.add(val);
                System.out
                        .println(Thread.currentThread().getName() + " 正在生产数据为:" + val + "    buffer目前大小为:" + buffer.size());
            } finally {
                mutex.release();
                // 生产者调用release,增加可以消费的数量
                canConsumerCount.release();
            }
    
        }
    }
    
    public class SemaphoreProducerComsumer1 {
    
        public static void main(String[] args) {
            final Buffer buffer = new Buffer();
            startProducer(buffer);
            startProducer(buffer);
            startConsumer(buffer);
            startConsumer(buffer);
    
        }
    
        public static void startProducer(final Buffer buffer) {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        while (true) {
                            buffer.put();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }
            }).start();
        }
    
        public static void startConsumer(final Buffer buffer) {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        while (true) {
                            buffer.get();
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                }
            }).start();
        }
    
    }
  • 相关阅读:
    Stalstack 连接管理配置
    Stalstack 安装
    Apache 错误整理
    Apache 服务常用命令
    Apache 优化
    Shell 常用技巧
    Nginx+keepalived做双机热备加tomcat负载均衡
    用Lighttpd做图片服务器
    rsync是类unix系统下的数据镜像备份工具
    redis+keeplived分布式缓存
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7703721.html
Copyright © 2011-2022 走看看