当前位置:网站首页>Blocking queue
Blocking queue
2022-04-22 08:16:00 【crazyK.】
One 、 What is blocking queue
From the name, we can see that blocking queue is a kind of queue , It's a kind of FIFO( fifo ) Data structure of , Unlike ordinary queues , It supports blocking addition and blocking deletion
Block to add : When the queue is full , Adding elements to the queue blocks the wait
Blocking the delete : When the queue is empty , Deleting or getting elements into the queue will be blocked

Two 、 Four groups of blocking queues API
| The way | Throw an exception | Does not throw an exception , There is a return value | Block waiting | Overtime waiting |
| add to | add() | offer() | put() | offer(e,timeout,TimeUnit) |
| remove | remove() | poll() | take() | poll(timeout,TimeUnit) |
| Get team leader elements | element() | peek() | - | - |
1. Throw an exception
public static void test1(){
// The size of the queue is filled in brackets , Look at the source code
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
for (int i = 0; i < 3; i++) {
System.out.println(blockingQueue.add(String.valueOf(i)));
}
// Get team leader elements
System.out.println(blockingQueue.element());
// The queue is full , Adding another element to the queue will throw an exception : Queue full --> The queue is full
//blockingQueue.add("hhh");
System.out.println("===================");
// Pop up the elements in the queue
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
// The queue is empty , Taking elements from the queue will throw an exception NoSuchElementException --> There is no element
//blockingQueue.remove();
}



2. Don't throw exceptions , There is a return value
public static void test2(){
// Similarly, fill in the size of the queue in parentheses
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
for (int i = 0; i < 3; i++) {
System.out.println(blockingQueue.offer(String.valueOf(i)));
}
// Get team leader elements
System.out.println(blockingQueue.peek());
// At this point the queue is full , Adding another element to the queue will return false
System.out.println(blockingQueue.offer("hhh"));
System.out.println("===========");
// Pop up the elements in the queue
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
// The queue is empty , Pop up the element again, and no exception will be thrown , return null
System.out.println(blockingQueue.poll());
}



3. Block waiting
public static void test3() throws InterruptedException {
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
for (int i = 0; i < 3; i++) {
blockingQueue.put(String.valueOf(i));
}
// The queue has no place , It will keep blocking
//blockingQueue.put("hhhh");
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
// The queue is empty , It will keep blocking
//System.out.println(blockingQueue.take());
}



4. Overtime waiting
public static void test4() throws InterruptedException {
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
for (int i = 0; i < 3; i++) {
System.out.println(blockingQueue.offer(String.valueOf(i)));
}
// The queue is full , Set the wait time , If the waiting time is up, the element has no position to insert , Will timeout and exit
//blockingQueue.offer("hhh",2, TimeUnit.SECONDS);
System.out.println("============");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
// The queue is empty , Set the wait time , If the waiting time is up, there are no elements in the queue , Will time out and return null
System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
}


3、 ... and 、 Synchronous queue SynchronousQueue
And others BlockingQueue Different
Synchronization queues do not store elements
In line put An element Must wait for this element take Only after leaving the queue put The next element
public class SynchronousQueueTest {
public static void main(String[] args) {
SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" put 1");
synchronousQueue.put("1");
System.out.println(Thread.currentThread().getName()+" put 2");
synchronousQueue.put("2");
System.out.println(Thread.currentThread().getName()+" put 3");
synchronousQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T1").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+" get "+synchronousQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T2").start();
}
}
版权声明
本文为[crazyK.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220651476594.html
边栏推荐
- 架构师评价当前软件行业现状及发展前景
- 网络原理二(上)
- OpenFeign之响应处理
- 16 way E1 optical transceiver + 4-way 100M Ethernet optical transceiver PDH optical transceiver 2m integrated service optical transceiver
- OpenCV对矩形填充透明颜色
- 雙光口1+1備份8路E1+2路千兆隔離網絡4路百兆隔離PDH光端機
- Provide 4-way E1 service ports, 4-way 100m isolation network, 2m E1 private network, multi service PDH optical transceiver
- pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- Double optical port protection 8e1 + 8-way physical isolation 100M Ethernet optical transceiver PDH optical transceiver
- Hanyuan hi tech PDH optical transceiver double optical port protection + 4-way E1 + 4-way Gigabit Network + 4-way 100m network optical transceiver
猜你喜欢

tf.keras.layers.Conv?D函数

提供4路E1业务口4路百兆隔离网络2M E1专网多业务PDH光端机

荧光标记的多肽/氨基酸(FITC修饰/AMC修饰)齐岳生物

OpenFeign超时设置

Q-Learning & Sarsa

VS Code 设置换行

架构师评价当前软件行业现状及发展前景

Shrio study notes (II)

Integrated service PDH optical transceiver 4-way Gigabit isolated Ethernet + 4-way E1 private network service 2m integrated service optical transceiver

微信小程序页面路由
随机推荐
FileNotFoundError: [Errno 2] No such file or directory
Dual optical port 1 + 1 backup 8-way E1 + 2-way Gigabit isolation network 4-way 100m isolation PDH optical transceiver
OpenCV读取网络摄像头视频并保存到本地
Selenium driver installation
Integrated service PDH optical transceiver 4-way Gigabit isolated Ethernet + 4-way E1 private network service 2m integrated service optical transceiver
ADB advanced commands
LabVIEW 2012中的收藏选板导入到LabVIEW 2013
OpenFeign的细节展示
汉源高科8E1专网4路百兆隔离网络PDH光端机E1专网业务16M业务光端机
ER 和 EER 模型
pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
pytest_ First class
Redis entry required
电信级双光口保护8E1+4路物理隔离千兆网络光端机1000M网络100M光端机
架构师评价当前软件行业现状及发展前景
软件测试面试题汇总
58技术沙龙第二十八期|安居客质量保障体系沙龙
MYSQL03_SQL的概述、规则和规范、基本的SELECT语句、显示表结构
monkey
牛客白月赛6【题解】