Java--JUC--BlockingQueueDemo(阻塞队列)

  1. 例子:BlockQueueDemo
  2. 栈和队列
  3. 阻塞队列
    1.  

       

       

       

       

        
  4. 阻塞队列的好处
    1.  

       

       

       
  5. 架构梳理和种类分析
    1.  
    2.  

       

       

       

       

       

       

       
  6. BlockQueue核心方法
    1.  

       

        
  7. 代码
    1. package com.model.queue;
      
      import jdk.nashorn.internal.ir.Block;
      
      import java.util.concurrent.ArrayBlockingQueue;
      import java.util.concurrent.BlockingQueue;
      import java.util.concurrent.TimeUnit;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/7 21:27
       */
      public class BlockingQueueDemo {
          public static void main(String[] args) throws InterruptedException {
              //阻塞队列
              BlockingQueue 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.add("x"));
      
              System.out.println(blockingQueue.remove());
              System.out.println(blockingQueue.remove());
              System.out.println(blockingQueue.remove());
      //        System.out.println(blockingQueue.remove());*/
      
           /*   System.out.println(blockingQueue.add("a"));
              System.out.println(blockingQueue.add("b"));
              System.out.println(blockingQueue.element());*/
      
      /*
              System.out.println(blockingQueue.offer("a"));
              System.out.println(blockingQueue.offer("c"));
              System.out.println(blockingQueue.offer("d"));
              System.out.println(blockingQueue.offer("f"));
              System.out.println(blockingQueue.poll());
              System.out.println(blockingQueue.poll());
              System.out.println(blockingQueue.poll());
              System.out.println(blockingQueue.poll());*/
      
          /*    //如果满了会一直等待阻塞
              blockingQueue.put("a");
              blockingQueue.put("a");
              blockingQueue.put("a");
      //        blockingQueue.put("a");
      
              //如果资源不够了就一直等待阻塞,知道有资源后在执行
              System.out.println(blockingQueue.take());
              System.out.println(blockingQueue.take());
              System.out.println(blockingQueue.take());
              System.out.println(blockingQueue.take());
      */
      //        等待资源,等待时间为3秒,3秒没有等到,就会部等待,去寻找其他 的资源
              System.out.println(blockingQueue.offer("a",3,TimeUnit.SECONDS));
      
          }
      }

       

        
posted @ 2021-06-07 21:50  张紫韩  阅读(123)  评论(0编辑  收藏  举报