Loading

四组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     }

 

posted @ 2020-04-14 16:39  Rzk  阅读(201)  评论(0编辑  收藏  举报