• 基于ReentrantLock通知唤醒的生产消费模式


    生产者消费者

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class TaskExecutor {
        private List<Integer> list = new ArrayList<>();
        private AtomicInteger count = new AtomicInteger();
        private ReentrantLock lock = new ReentrantLock();
        /**
         * 积压消息容量
         */
        private int capacity;
        private Condition c1 = lock.newCondition();
        private Condition c2 = lock.newCondition();
    
        /**
         * 可读索引
         */
        private int read;
    
        public TaskExecutor(int capacity) {
            this.capacity = capacity;
        }
    
        public Integer product() {
            Integer value = null;
            lock.lock();
            try {
                while (list.size() >= capacity) {
                    c1.await();
                }
                value = count.incrementAndGet();
                list.add(value);
                read++;
                c2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
            return value;
        }
    
        public Integer consume() {
            Integer value = null;
            lock.lock();
            try {
                while (list.size() <= 0) {
                    c2.await();
                }
                read--;
                value = list.remove(read);
    
                c1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
            return value;
        }
    
        private Integer getSize() {
            lock.lock();
            try {
                return list.size();
            } finally {
                lock.unlock();
            }
        }
    
        public static void main(String[] args) {
            TaskExecutor taskExecutor = new TaskExecutor(20);
            new Thread(()-> {
                for (;;) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 生产: " + taskExecutor.product() + " 积压: " + taskExecutor.getSize());
                }
            }, "producer").start();
            new Thread(()-> {
                for (;;) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 消费: " + taskExecutor.consume() + " 积压: " + taskExecutor.getSize());
                }
            }, "consumer").start();
    
        }
    }
    
  • 相关阅读:
    String类的substring方法
    postman绕过登录,进行接口测试的方法
    Dubbo
    那些吊炸天的互联网名词
    版本控制工具git
    Ubunto20.04 sudo apt-get update 出现目标被重置多次!
    ubuntu环境下搭建Hadoop集群中必须需要注意的问题
    Python作业---内置数据类型
    python作业完成简单的文件操作
    python3实现计算器
  • 原文地址:https://www.cnblogs.com/snow-wolf-1/p/15156526.html
Copyright © 2020-2023  润新知