• 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();
        }
    
    }
  • 相关阅读:
    angular安装指定版本
    Fluttter通过按钮来打开抽屉Drawer或者endDrawer
    angular中的animation动画
    flutter pubspec.yaml配置文件详解
    angular-cli卸载安装
    angular的项目基本配置的了解
    angular使用代理解决跨域
    IOS开发之UI布局
    用Objective-C写了一个简单的批量更改文件名的程序
    使用Objective-C 计算代码运行时间
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7703721.html
Copyright © 2020-2023  润新知