java-JUC--BlockingQueueDemo(阻塞队列)
Posted 张紫韩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-JUC--BlockingQueueDemo(阻塞队列)相关的知识,希望对你有一定的参考价值。
- 例子:BlockQueueDemo
- 栈和队列
- 阻塞队列
-
- 阻塞队列的好处
-
- 架构梳理和种类分析
- BlockQueue核心方法
-
- 代码
-
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(阻塞队列)的主要内容,如果未能解决你的问题,请参考以下文章