当前位置:网站首页>Blocking queue
Blocking queue
2022-07-27 14:07: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 {
// Create a blocking queue - queue ( fifo ) Stack ( Last in, first out )
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
// The first group add、remove Throw exceptions element Check
first(blockingQueue);
// The second group offer、poll Return value false and null,peek Check
second(blockingQueue);
// The third group put、take Blocking
third(blockingQueue);
// Group 4 offer、poll Multi parameter timeout - Blocking - Timeout long return value false and 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?
- How to make computers have public IP
- Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
- 认知篇----硬件工程师的成才之路之经典
- West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion
- 现在还来得及参加9月份的PMP考试吗?
- VSCode -- 创建模板文件
- WPF visifire.charts4.6.1 tutorial with source code
- 【笔记】逻辑斯蒂回归
- [training day3] delete [simulation]
猜你喜欢

West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion

阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%

592. Fraction addition and subtraction

Research on automatic classification of electronic medical records based on unbalanced data
idea Gradle7.0+ :Could not find method compile()

The salary level of programmers in various countries is a little miserable

Wechat campus laundry applet graduation design finished product (4) opening report

Small program completion work wechat campus laundry small program graduation design finished product (2) small program function

融合迁移学习与文本增强的中文成语隐喻知识识别与关联研究

13. User web layer services (I)
随机推荐
Leetcode Tencent selected exercises 50 questions -054. spiral matrix
知识关联视角下金融证券知识图谱构建与相关股票发现
利用C语言实现URL解析的基本方法之优秀
[training day3] delete [simulation]
小程序毕设作品之微信校园洗衣小程序毕业设计成品(4)开题报告
West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion
井贤栋等蚂蚁集团高管不再担任阿里合伙人 确保独立决策
NoSQL -- three theoretical cornerstones of NoSQL -- cap -- Base -- final consistency
Alibaba's latest equity exposure: Softbank holds 23.9% and caichongxin holds 1.4%
Redis implements the browsing history module
RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
基于C语言的LR1编译器设计
第3章业务功能开发(查看线索明细)
How to make computers have public IP
为什么会出现Script file ‘D:\Anaconda3\envs\paddle_env\Scripts\pip-script.py‘ is not present.
Small program completion work wechat campus laundry small program graduation design finished product (2) small program function
Flat die cutting machine
基于预训练模型的多标签专利分类研究
Application layer World Wide Web WWW
Software testing system architecture designer concise tutorial | software testing