zoukankan      html  css  js  c++  java
  • ArrayBlockingQueue

    /**
     *  add    超过capacity会抛出异常     remove  队列为空会抛出异常      add和remove底层也是调用的offer和poll方法
     *
     *  offer  超过capacity会返回false    poll   队列为空会返回null
     *
     *  put    超过capacity会阻塞         take 队列为空会阻塞
     *
     */
    public class ArrayBlockingQueueTest {
    
        static ArrayBlockingQueue<String> queue = new ArrayBlockingQueue(2);
    
        public static void main(String[] args) throws InterruptedException {
    //        testAdd();
    //        testRemove();
    
    //        testOffer();
    //        testPoll();
    
    //        testPut();
    //        testTake();
    
        }
    
    
        /**
         * 超过capacity会抛出异常
         */
        public static void testAdd() {
            queue.add("hello1");
            queue.add("hello2");
            queue.add("hello3");
        }
    
        /**
         * 队列为空抛出异常
         */
        public static void testRemove() {
            queue.remove();
    
        }
    
        /**
         * 超过capacity会返回false
         */
        public static void testOffer() {
            queue.offer("hello1");
            queue.offer("hello2");
            System.out.println(queue.offer("hello3"));
        }
    
        /**
         * 队列为空返回null
         */
        public static void testPoll() {
            System.out.println(queue.poll());
        }
    
        /**
         * 超过capacity会阻塞
         */
        public static void testPut() throws InterruptedException {
            queue.put("hello1");
            queue.put("hello2");
            queue.put("hello3");
        }
    
    
        /**
         * 队列为空会阻塞
         */
        public static void testTake() throws InterruptedException {
            queue.take();
    
        }
    
    }
  • 相关阅读:
    SpringBoot exception异常处理机制源码解析
    集群时间同步
    VMware 克隆的相关设置
    MySQL安装
    MogliFS与spring mvc结合简单示例
    MogileFS与spring结合
    MogileFS表说明
    使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法
    IAT HOOK 简单实现
    进程加载_模块隐藏
  • 原文地址:https://www.cnblogs.com/moris5013/p/12043687.html
Copyright © 2011-2022 走看看