当前位置:网站首页>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));
}
}
边栏推荐
- Travel notes from July 11 to August 1, 2022
- 认知篇----硬件工程师的成才之路之经典
- 【图论】负环
- 小程序毕设作品之微信校园洗衣小程序毕业设计成品(7)中期检查报告
- 面向不平衡数据的电子病历自动分类研究
- 特征工程中的缩放和编码的方法总结
- Weice biological IPO meeting: annual revenue of 1.26 billion Ruihong investment and Yaohe medicine are shareholders
- 关于max做动画的一些关键信息(shift+v)
- 递归方法实现最大公约数
- 阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%
猜你喜欢

记账软件如何查看收入支出

Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers

软考 系统架构设计师 简明教程 | 软件系统建模
idea Gradle7.0+ :Could not find method compile()

Interview eight part essay · TCP protocol

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

C#测量工具示意图

There is no need for semantic segmentation of annotation data! Eth & Leuven University proposed maskdistill, using transformer for unsupervised semantic segmentation, SOTA

Some key information about Max animation (shift+v)

Shell编程规范与变量
随机推荐
小程序毕设作品之微信校园洗衣小程序毕业设计成品(3)后台功能
RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
Thinkphp+ pagoda operation environment realizes scheduled tasks
基于预训练模型的多标签专利分类研究
We should learn to check the documented instructions of technical details
[idea] set to extract serialVersionUID
Weice biological IPO meeting: annual revenue of 1.26 billion Ruihong investment and Yaohe medicine are shareholders
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Group division and characteristic analysis of depression patients based on online consultation records
GoPro access - control and preview GoPro according to GoPro official document /demo
Onnxruntime [reasoning framework, which users can easily use to run an onnx model]
In the "meta cosmic space", utonmos will open the digital world of the combination of virtual and real
Named entity recognition of Chinese electronic medical records based on Roberta WwM dynamic fusion model
ONNXRuntime【推理框架,用户可以非常便利的用其运行一个onnx模型】
NFT 的 10 种实际用途
文旅数藏 | 用艺术的方式游云南
Zhishang technology IPO meeting: annual revenue of 600million, book value of accounts receivable of 270Million
Sword finger offer II 041. Average value of sliding window
592. 分数加减运算
Jing Xiandong and other senior executives of ant group no longer serve as Alibaba partners to ensure independent decision-making