当前位置:网站首页>Handwriting blocking queue: condition + lock
Handwriting blocking queue: condition + lock
2022-07-05 12:16:00 【The king of early rising】
Don't talk much , Go straight to the code
public class BlockingQueue<T> {
private final ArrayDeque<T> blockingQueue;
private int capacity; // The maximum length allowed for the queue
private final Condition notFull;
private final Condition notEmpty;
Lock lock = new ReentrantLock();
public BlockingQueue(int capacity) {
notEmpty = lock.newCondition();
notFull = lock.newCondition();
this.capacity = capacity;
blockingQueue = new ArrayDeque<>(capacity);
}
public void insert(T t){ // Method of inserting task
lock.lock();
try {
while (blockingQueue.size() == capacity){
notFull.await();
blockingQueue.add(t);
notEmpty.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
lock.unlock();
}
}
public T remove() throws InterruptedException { // Method of getting task
try {
while(blockingQueue.size() == 0){
notEmpty.await();
}
T t = blockingQueue.remove();
notFull.signal();
return t;
} finally{
lock.unlock();
}
}
}Keep early hours , take care ; The king of early rising wishes you a thousand miles a day !
边栏推荐
- Simple production of wechat applet cloud development authorization login
- MVVM framework part I lifecycle
- Pytorch weight decay and dropout
- How to clear floating?
- GPS數據格式轉換[通俗易懂]
- 1 plug-in to handle advertisements in web pages
- [singleshotmultiboxdetector (SSD, single step multi frame target detection)]
- MySQL multi table operation
- A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
- 嵌入式软件架构设计-消息交互
猜你喜欢
![[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]](/img/ad/b96e9319212cf2724e0a640109665d.png)
[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]
![[cloud native | kubernetes] actual battle of ingress case (13)](/img/1a/9404f6dcedd15827fa45f8f6f4c093.png)
[cloud native | kubernetes] actual battle of ingress case (13)

Redis master-slave mode

HiEngine:可媲美本地的云原生内存数据库引擎

Seven ways to achieve vertical centering

Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle

Simple production of wechat applet cloud development authorization login

Get all stock data of big a

Why learn harmonyos and how to get started quickly?

嵌入式软件架构设计-消息交互
随机推荐
MySQL index - extended data
Image hyperspectral experiment: srcnn/fsrcnn
MySQL installation, Windows version
Understand kotlin from the perspective of an architect
POJ-2499 Binary Tree
mmclassification 训练自定义数据
Conversion du format de données GPS [facile à comprendre]
[configuration method of win11 multi-user simultaneous login remote desktop]
Uniapp + unicloud + Unipay realize wechat applet payment function
Swift - enables textview to be highly adaptive
Recyclerview paging slide
Xi IO flow
Application of a class of identities (vandermond convolution and hypergeometric functions)
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
Multi table operation - Auto Association query
POJ-2499 Binary Tree
Principle and performance analysis of lepton lossless compression
Automated test lifecycle
Linux安装部署LAMP(Apache+MySQL+PHP)
MySQL stored procedure