当前位置:网站首页>阻塞队列BlockingQueue
阻塞队列BlockingQueue
2022-07-27 12:54:00 【linsa_pursuer】
package juc;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
//创建阻塞队列-队列(先进先出)栈(后进先出)
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
//第一组add、remove抛异常element检查
first(blockingQueue);
//第二组offer、poll返回值false和null,peek检查
second(blockingQueue);
//第三组put、take阻塞
third(blockingQueue);
//第四组offer、poll多参数超时-阻塞-超时长返回值false和null
four(blockingQueue);
}
private static void first(BlockingQueue<String> blockingQueue){
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c"));
System.out.println(blockingQueue.element());
//System.out.println(blockingQueue.add("w"));//IllegalStateException: Queue full
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//System.out.println(blockingQueue.remove());//NoSuchElementException
}
private static void second(BlockingQueue<String> blockingQueue){
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("www"));
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
}
private static void third(BlockingQueue<String> blockingQueue) throws InterruptedException {
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
//blockingQueue.put("w");
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.take());
}
private static void four(BlockingQueue<String> blockingQueue) throws InterruptedException {
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("w",3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
}
}
边栏推荐
- The universe has no end. Can utonmos shine the meta universe into reality?
- Download address of each version of libtorch
- RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
- 【每日一题】1206. 设计跳表
- Accuracy improvement method: efficient visual transformer framework of adaptive tokens (open source)
- Hcip - OSPF comprehensive experiment
- What are the benefits of taking NPDP
- SNMP (Simple Network Management Protocol)
- egg-swagger-doc 图形验证码解决方案
- Egg swagger doc graphic verification code solution
猜你喜欢

Motion attitude control system of DOF pan tilt based on stm32

What are the benefits of taking NPDP

小程序毕设作品之微信校园洗衣小程序毕业设计成品(1)开发概要

Thinkphp+ pagoda operation environment realizes scheduled tasks

在“元宇宙空间”UTONMOS将打开虚实结合的数字世界

Zoom, translation and rotation of OpenCV image

剑指Offer 07 重建二叉树 -- 从中序与后序遍历序列构造二叉树

小程序毕设作品之微信校园洗衣小程序毕业设计成品(6)开题答辩PPT

深度置信网络(DBN)【经典的DBN网络结构是由若干层 RBM(受限波尔兹曼机)和一层 BP 组成的一种深层神经网络】

JS module, closure application
随机推荐
微策生物IPO过会:年营收12.6亿 睿泓投资与耀合医药是股东
【每日一题】1206. 设计跳表
LeetCode报错及其解决方案
Redis cluster setup - use docker to quickly build a test redis cluster
【图论】负环
【idea】设置提取serialVersionUID
Converter registration of easyexcel
宇宙没有尽头,UTONMOS能否将元宇宙照进现实?
Use recyclerview to realize the left sliding menu of the list
Leetcode error reporting and its solution
2022acm summer training weekly report (IV)
MySQL high availability practical solution MHA
Meshlab farthest point sampling (FPS)
opencv图像的缩放平移及旋转
Wechat campus laundry applet graduation design finished product (4) opening report
Construction and application of industrial knowledge atlas (2): representation and modeling of commodity knowledge
Structural thinking
Wechat campus laundry applet graduation design finished product (7) Interim inspection report
达科为生物IPO过会:年营收8.37亿 吴庆军父女为实控人
基于C语言实现线性表的建立、插入、删除、查找等基本操作