当前位置:网站首页>BlockingQueue four sets of APIs
BlockingQueue four sets of APIs
2022-06-22 06:18:00 【Kuxiaoya】
Blocking queues
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mOzXsHGu-1654973647624)(C:\Users\38492\AppData\Local\Temp\1654970501452.png)]](/img/c2/f116643a0c5de24f2b95b3f7ce1595.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-xd30NitP-1654973647634)(C:\Users\38492\AppData\Local\Temp\1654970625596.png)]](/img/f1/72612c7df02f3b28b843328cc121a3.png)
Under what circumstances will we use Blocking queues ?
Multithreading concurrent processing , Thread pool !
Be sure to learn to use queues
add to 、 remove
BlockingQueue( Blocking queues ) Four groups API
| The way | Throw an exception | There is a return value , Don't throw exceptions | Block waiting | Overtime waiting |
|---|---|---|---|---|
| add to | add | offer | put | offer |
| remove | remove | poll | take | poll |
| First element of inspection team | element | peek | - | - |
The first group :( Throw an exception )
package bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test {
public static void main(String[] args) {
test1();
}
/** * Throw an exception */
public static void test1(){
// The size of the queue
ArrayBlockingQueue 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.element());// See who is the head of the team
// The queue is 3, We add a fourth element : IllegalStateException Throw an exception !( Illegal status exception )
//System.out.println(blockingQueue.add("d"));
System.out.println("----------------");
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//NoSuchElementException Throw an exception !( There is no such element exception )
//System.out.println(blockingQueue.remove());
}
}
The second group ( There is a return value , Don't throw exceptions )
package bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test {
public static void main(String[] args) {
test2();
}
/** * There is a return value , Don't throw exceptions */
public static void test2(){
// The size of the queue
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
//System.out.println(blockingQueue.peek());// See who is the head of the team
//false Don't throw exceptions !
//System.out.println(blockingQueue.offer("d"));
System.out.println("----------------");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//null Don't throw exceptions !
System.out.println(blockingQueue.poll());
}
}
The third group ( Block waiting )
package bq;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Test {
public static void main(String[] args) throws InterruptedException {
test3();
}
/** * wait for , Blocking ( Keep blocking ) */
public static void test3() throws InterruptedException {
// The size of the queue
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
// Keep blocking
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
//blockingQueue.put("d"); // The queue has no place , Keep blocking
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.take()); // There is no such element , Keep blocking
}
}
Group 4 ( Overtime waiting )
package bq;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
test4();
}
/** * wait for , Blocking ( Time out blocking ) */
public static void test4() throws InterruptedException {
// The size of the queue
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
//System.out.println(blockingQueue.offer("d", 2,TimeUnit.SECONDS));// Wait more than 2 Seconds to exit
System.out.println("--------------");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));// Wait more than 2 Seconds to exit
}
}
边栏推荐
猜你喜欢
随机推荐
单细胞论文记录(part7)--DL and alignment of spatially resolved single-cell transcriptomes with Tangram
On the matrix order of MNIST linear model
Huiding technology gr551x series development board supports openharmony
SQL 注入漏洞(十三)base64注入
SQL 注入漏洞(十)二次注入
tp6链接sqlserver,php链接sqlserver,linux离线安装与部署超详细
Shengxin visualization (Part3) -- violin diagram
Logback custom pattern parameter resolution
Vertical maximum and minimum and horizontal maximum and minimum greatest(), least(), max(), min()
【Rust笔记】03-引用
Pyg tutorial (7): dissecting neighborhood aggregation
CGIC file upload - rookie notes
Research on automatic landing control system of carrier aircraft
MiniGUl 1.1.0版本引入的新GDI功能和函数(二)
【Rust笔记】04-表达式
Modeling and Simulation of Radar Seeker Servo System
Machine learning concept sorting (no formula)
单细胞论文记录(part11)--ClusterMap for multi-scale clustering analysis of spatial gene expression
Flink核心功能和原理
878. 第 N 个神奇数字 数学+二分








