当前位置:网站首页>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 !
边栏推荐
- Video networkstate property
- 1 plug-in to handle advertisements in web pages
- Instance + source code = see through 128 traps
- Take you two minutes to quickly master the route and navigation of flutter
- 你做自动化测试为什么总是失败?
- Matlab superpixels function (2D super pixel over segmentation of image)
- 投资理财适合女生吗?女生可以买哪些理财产品?
- Tabbar configuration at the bottom of wechat applet
- Open3d mesh (surface) coloring
- [upsampling method opencv interpolation]
猜你喜欢

互联网公司实习岗位选择与简易版职业发展规划

Matlab boundarymask function (find the boundary of the divided area)

Tabbar configuration at the bottom of wechat applet

Pytorch weight decay and dropout

One article tells the latest and complete learning materials of flutter

Why learn harmonyos and how to get started quickly?

Uniapp + unicloud + Unipay realize wechat applet payment function

Seven ways to achieve vertical centering

多表操作-自关联查询

强化学习-学习笔记3 | 策略学习
随机推荐
自动化测试生命周期
无线WIFI学习型8路发射遥控模块
[upsampling method opencv interpolation]
Swift - enables textview to be highly adaptive
7月华清学习-1
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
Principle of persistence mechanism of redis
Automated test lifecycle
[configuration method of win11 multi-user simultaneous login remote desktop]
Video networkState 属性
多表操作-自关联查询
Take you two minutes to quickly master the route and navigation of flutter
Linux Installation and deployment lamp (apache+mysql+php)
Time tools
Vscode shortcut key
Principle of redis cluster mode
GPS數據格式轉換[通俗易懂]
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
mmclassification 训练自定义数据