• 四组API


    方式              抛出异常     有返回值,不抛出异常      阻塞,等待     超时等待         
    添加 add offet() put()  offer(,,)
    移除   remove poll() take() poll(,)
    检测对首元素 element peek .. ..

    第一

     1 //抛出异常
     2     public static void Test1(){
     3         ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
     4 
     5         System.out.println(blockingQueue.add("A"));
     6         System.out.println(blockingQueue.add("B"));
     7         System.out.println(blockingQueue.add("C"));
     8         //System.out.println(blockingQueue.add("A"));
     9         // java.lang.IllegalStateException: Queue full 队列已满
    10         System.out.println("===");
    11 
    12         System.out.println(blockingQueue.element());//查看队列个元素首是谁
    13 
    14         System.out.println(blockingQueue.remove());
    15         System.out.println(blockingQueue.remove());
    16         System.out.println(blockingQueue.remove());
    17         //java.util.NoSuchElementException  这里面没有元素
    18         //System.out.println(blockingQueue.remove());
    19 
    20     }

    第二

     1  //有返回值,没有异常
     2     public static void Test2(){
     3         //队列大小
     4         ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
     5         System.out.println(blockingQueue.offer("a"));
     6         System.out.println(blockingQueue.offer("b"));
     7         System.out.println(blockingQueue.offer("c"));
     8         //System.out.println(blockingQueue.offer("c"));//返回false  不会抛出异常
     9 
    10         System.out.println(blockingQueue.peek());
    11 
    12         System.out.println("======================");
    13         System.out.println(blockingQueue.poll());
    14         System.out.println(blockingQueue.poll());
    15         System.out.println(blockingQueue.poll());
    16         //System.out.println(blockingQueue.poll());//返回一个null  不会抛出异常
    17     }

    第三

     1  //等待,阻塞(一直)
     2     public static void Test3() throws InterruptedException {
     3         //队列大小
     4         ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
     5         blockingQueue.put("a");
     6         blockingQueue.put("b");
     7         blockingQueue.put("c");
     8         //blockingQueue.put("d");//队列 没有位置,程序就会一直等待  下面的程序不会执行
     9         //System.out.println(blockingQueue.offer("c"));//返回false  不会抛出异常
    10 
    11         System.out.println(blockingQueue.peek());
    12 
    13         System.out.println("======================");
    14         System.out.println(blockingQueue.take());
    15         System.out.println(blockingQueue.take());
    16         System.out.println(blockingQueue.take());
    17         System.out.println(blockingQueue.take());//找不到元素,会一直等待,一直阻塞状态
    18     }

    第四

     1  //等待,阻塞(等待超时)
     2     public static void Test4() throws InterruptedException {
     3         //队列大小
     4         ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
     5         blockingQueue.offer("a");
     6         blockingQueue.offer("b");
     7         blockingQueue.offer("c");
     8         blockingQueue.offer("b",2, TimeUnit.SECONDS);
     9         //       blockingQueue.offer("b",2, TimeUnit.SECONDS);//等待超過兩秒就退出
    10 
    11         System.out.println("===============");
    12 
    13         System.out.println(blockingQueue.poll());
    14         System.out.println(blockingQueue.poll());
    15         System.out.println(blockingQueue.poll());
    16         System.out.println(blockingQueue.poll(3, TimeUnit.SECONDS));
    17         //blockingQueue.poll(3, TimeUnit.SECONDS) 等待超過三秒就推出
    18     }
  • 相关阅读:
    cocos2dx android java调用C++
    cocos2dx android c++调用java
    cocos2dx android collection
    cocos2dx学习之windows android环境搭建
    windows phone 8.0 与 windows phone7.1区别
    C#文件同步工具教程
    Web2.0时代,RSS你会用了吗?(技术实现总结)
    介绍一篇关于session的好文章,写的很详细(jspservlet 技术)
    利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
    css
  • 原文地址:https://www.cnblogs.com/rzkwz/p/12698946.html
Copyright © 2020-2023  润新知