其他分享
首页 > 其他分享> > BlockingQueue阻塞队列

BlockingQueue阻塞队列

作者:互联网

BlockingQueue阻塞队列

 

BlockingQueue的四组API

/**BlockQueue的四组API
* 1.抛出异常
* 2.有返回值,不抛出异常
* 3.阻塞等待
* 4.超时等待
*/
public class BlockQueueTest {
   public static void main(String[] args) throws InterruptedException {
       test03();
  }

   /**抛出异常
    * */
   public static void test01(){
       ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);//队列的长度
       System.out.println(blockingQueue.add("a"));
       System.out.println(blockingQueue.add("b"));
       System.out.println(blockingQueue.add("c"));
       //获取队首元素
       System.out.println(blockingQueue.element());
       //IllegalStateException: Queue full 抛出异常,列满
                 //blockingQueue.add("d");
       System.out.println(blockingQueue.remove());
       System.out.println(blockingQueue.remove());
       System.out.println(blockingQueue.remove());
       //.NoSuchElementException 队空
       //System.out.println(blockingQueue.remove());

  }
   /**不抛出异常,有返回值
    * */
   public static void test02(){
       ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
       System.out.println(blockingQueue.offer("a"));
       System.out.println(blockingQueue.offer("b"));
       System.out.println(blockingQueue.offer("c"));
       //获取队首元素
       System.out.println(blockingQueue.peek());
       //有返回值,不抛出异常
      // System.out.println(blockingQueue.offer("d"));
       System.out.println(blockingQueue.poll());
       System.out.println(blockingQueue.poll());
       System.out.println(blockingQueue.poll());
       //有返回值,不抛出异常
       //System.out.println(blockingQueue.poll());
  }
   /**阻塞等待,一直阻塞
    * */
   public static void test03() throws InterruptedException {
       ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
       arrayBlockingQueue.put("a");
       arrayBlockingQueue.put("b");
       arrayBlockingQueue.put("c");
       //队满,阻塞等待,一直等待
       //arrayBlockingQueue.put("d");
       System.out.println(arrayBlockingQueue.take());
       System.out.println(arrayBlockingQueue.take());
       System.out.println(arrayBlockingQueue.take());
       //队空,阻塞等待,一直等待
       //System.out.println(arrayBlockingQueue.take());
  }
   /**
    *超时等待,但不会一直等待,阻塞时间内等待,之后退出
    */
   public static void test04() throws InterruptedException {
       ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
       System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));
       System.out.println(blockingQueue.offer("b", 2, TimeUnit.SECONDS));
       System.out.println(blockingQueue.offer("c", 2, TimeUnit.SECONDS));
       //阻塞两秒,队满返回false退出
       // System.out.println(blockingQueue.offer("c", 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));
       //阻塞异常,阻塞两秒,队空返回null
       // System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
  }
}
 

标签:队列,System,阻塞,TimeUnit,println,arrayBlockingQueue,BlockingQueue,out,blockingQueue
来源: https://www.cnblogs.com/wfy-studying/p/16161862.html