当前位置:网站首页>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 !
边栏推荐
- Network five whip
- Time tools
- byte2String、string2Byte
- 一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
- 【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
- Hiengine: comparable to the local cloud native memory database engine
- Image hyperspectral experiment: srcnn/fsrcnn
- Redis master-slave mode
- Vscode shortcut key
- Linux安装部署LAMP(Apache+MySQL+PHP)
猜你喜欢
Flutter2 heavy release supports web and desktop applications
Matlab superpixels function (2D super pixel over segmentation of image)
[pytorch pre training model modification, addition and deletion of specific layers]
Check the debug port information in rancher and do idea remote JVM debug
[cloud native | kubernetes] actual battle of ingress case (13)
Automated test lifecycle
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
报错ModuleNotFoundError: No module named ‘cv2.aruco‘
多表操作-自关联查询
什么是数字化存在?数字化转型要先从数字化存在开始
随机推荐
无线WIFI学习型8路发射遥控模块
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
Hash tag usage in redis cluster
Matlab boundarymask function (find the boundary of the divided area)
GPS数据格式转换[通俗易懂]
Understand kotlin from the perspective of an architect
Deep discussion on the decoding of sent protocol
互联网公司实习岗位选择与简易版职业发展规划
JS for loop number exception
vscode快捷键
[yolov5.yaml parsing]
语义分割实验:Unet网络/MSRC2数据集
HiEngine:可媲美本地的云原生内存数据库引擎
[cloud native | kubernetes] actual battle of ingress case (13)
Basic operations of MySQL data table, addition, deletion and modification & DML
Get data from the database when using JMeter for database assertion
Intern position selection and simplified career development planning in Internet companies
Flutter2 heavy release supports web and desktop applications
PXE启动配置及原理