Java教程

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

本文主要是介绍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));
      
          }
      }

       

        
这篇关于java-JUC--BlockingQueueDemo(阻塞队列)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!