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

  1. 例子:BlockQueueDemo
  2. 栈和队列
  3. 阻塞队列
    1. java-JUC--BlockingQueueDemo(阻塞队列)
    2. java-JUC--BlockingQueueDemo(阻塞队列)

       

       

       

       

       

        
  4. 阻塞队列的好处
    1.  java-JUC--BlockingQueueDemo(阻塞队列)

       

       

       
  5. 架构梳理和种类分析
    1.  java-JUC--BlockingQueueDemo(阻塞队列)
    2. java-JUC--BlockingQueueDemo(阻塞队列)

       

       

       

       

       

       

       

       
  6. BlockQueue核心方法
    1. java-JUC--BlockingQueueDemo(阻塞队列)

       

       

        
  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));
      
          }
      }

       

        
上一篇:JUC并发编程二:Lock锁(重点)


下一篇:各种锁策略,JUC里面的信号量、计数器、循环屏障,HashMap总结