• 阻塞队列-自定义


    参考ArrayBlockingQueue

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class DemoBlockList {
        private ReentrantLock lock = new ReentrantLock();
    
        private Condition isEmpty = lock.newCondition();
    
        private Condition isFull = lock.newCondition();
    
        private List<String> list;
        private int count;
    
        public DemoBlockList(int max) {
            this.list = new ArrayList<>(max);
            this.count = max;
        }
    
        public void put(String val) throws InterruptedException {
            ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                while (list.size() == count) {
                    this.isFull.await();
                }
                list.add(val);
                count++;
                this.isEmpty.signalAll();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        public String take() throws InterruptedException {
            ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                while (list.size() == 0) {
                    this.isEmpty.await();
                }
                this.isFull.signalAll();
                count--;
                return list.remove(0);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
            return null;
        }
    }
        public static void main(String[] args) {
    
            DemoBlockList demoBlockList = new DemoBlockList(10);
            List<Thread> pList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                Thread p = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int i = 0;
                        while (true) {
                            try {
                                Thread.sleep(1000);
                                String val = Thread.currentThread().getName() + ",值=" + i;
                                demoBlockList.put(val);
                                System.out.println(Thread.currentThread().getName() + "存储数据value=" + val);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            i++;
                        }
                    }
                });
                p.setName("生产者线程-" + i);
                pList.add(p);
            }
            for (int i = 0; i < 10; i++) {
                pList.get(i).start();
            }
            List<Thread> cList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                Thread c = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int i = 0;
                        while (true) {
                            try {
                                Thread.sleep(500);
                                String s = demoBlockList.take();
                                System.out.println(Thread.currentThread().getName() + "-获得数据value=" + s);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            i++;
                        }
                    }
                });
                c.setName("消费者线程-" + i);
                cList.add(c);
            }
            for (int i = 0; i < 10; i++) {
                cList.get(i).start();
            }
    }
  • 相关阅读:
    预览图片功能 直接复制就OK
    记录:express返回自定义http状态吗
    Git tag 给当前分支打标签
    css双飞翼和圣杯布局
    简单模拟MVVM数据双向绑定
    JS的自定义事件(观察者模式)
    js模拟触发事件
    QueryString和BASE64
    WebResource.axd文件的配置和使用
    处理json中的异常字符
  • 原文地址:https://www.cnblogs.com/use-D/p/13301581.html
Copyright © 2020-2023  润新知