zoukankan      html  css  js  c++  java
  • 9. 阻塞队列

    BlockingQueue

    • 使用场景:多并发处理,线程池

    阻塞队列的使用

    四组API

    方式 抛出异常 有返回值,不抛异常 阻塞等待 超时等待
    添加 add offer put offer( , , )
    删除 remove poll take poll( , )
    检测队列首部 element peek

    代码测试

    package pers.vincent.matrix.subject.bq;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.TimeUnit;
    
    /**
     * 阻塞队列
     */
    public class BlockingQueueTest {
    
        public static void main(String[] args) throws InterruptedException {
            test4();
        }
    
        /**
         * add / remove 抛出异常
         * 检测队列首部: element
         */
        public static void test1(){
            ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);
    
            blockingQueue.add("a");
            blockingQueue.add("b");
            blockingQueue.add("c");
            blockingQueue.add("d");
    
            System.out.println("===================================");
    
            System.out.println(blockingQueue.element());
    
            System.out.println(blockingQueue.remove());
            System.out.println(blockingQueue.remove());
            System.out.println(blockingQueue.remove());
            System.out.println(blockingQueue.remove());
        }
    
        /**
         * offer / poll 不抛异常
         * peek 检索队列首部
         */
        public static void test2(){
            ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);
    
            blockingQueue.offer("a");
            blockingQueue.offer("b");
            blockingQueue.offer("c");
            blockingQueue.offer("d");
    
            System.out.println("===================================");
    
            System.out.println(blockingQueue.peek());
    
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
        }
    
        /**
         * put / take 阻塞
         *
         * @throws InterruptedException
         */
        public static void test3() throws InterruptedException {
            ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);
    
            blockingQueue.put("a");
            blockingQueue.put("b");
            blockingQueue.put("c");
    
            System.out.println("===================================");
    
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
        }
    
        public static void test4() throws InterruptedException {
            ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
    
            blockingQueue.offer("a", 2, TimeUnit.SECONDS);
            blockingQueue.offer("b", 2, TimeUnit.SECONDS);
            blockingQueue.offer("c", 2, TimeUnit.SECONDS);
    
            System.out.println("===================================");
    
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        }
    }
    

    SynchronousQueue 同步队列

    SynchronousQueue 不存储元素,只要往里面put了就必须得take之后才能再put值进去

  • 相关阅读:
    linux上搭建tingproxy服务
    windows上搭建linux系统
    PHP将图片流存为图片文件
    openLayer矩形框选要素,展示要素属性
    点击选中的要素,更换标注图片,并添加文本标注
    openLayer点击要素获取对应的属性信息
    openLayer实现放大缩小
    openLayer的切换中心点、定位功能
    使用openLayer加载arcgis中的地图
    openLayer实现两个地图联动
  • 原文地址:https://www.cnblogs.com/blackBlog/p/13451483.html
Copyright © 2011-2022 走看看