当前位置:网站首页>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 !
边栏推荐
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
- 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
- [hdu 2096] Xiaoming a+b
- JS for loop number exception
- Deep discussion on the decoding of sent protocol
- 只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
- II. Data type
- Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
- [untitled]
- Check the debug port information in rancher and do idea remote JVM debug
猜你喜欢
![[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]

Network five whip

Tabbar configuration at the bottom of wechat applet
![[loss functions of L1, L2 and smooth L1]](/img/c6/27eab1175766b77d4f030b691670c0.png)
[loss functions of L1, L2 and smooth L1]
![[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]](/img/1a/dd7453bc5afc6458334ea08aed7998.png)
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]

报错ModuleNotFoundError: No module named ‘cv2.aruco‘

codeforces每日5题(均1700)-第五天

强化学习-学习笔记3 | 策略学习

Master the new features of fluent 2.10

Troubleshooting of high memory usage of redis in a production environment
随机推荐
MySQL regular expression
PXE启动配置及原理
Principle of persistence mechanism of redis
POJ-2499 Binary Tree
Intern position selection and simplified career development planning in Internet companies
[untitled]
Which domestic cloud management platform manufacturer is good in 2022? Why?
How can beginners learn flutter efficiently?
Recyclerview paging slide
Complete activity switching according to sliding
Semantic segmentation experiment: UNET network /msrc2 dataset
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
Codeforces Round #804 (Div. 2)
Halcon 模板匹配实战代码(一)
Deep discussion on the decoding of sent protocol
JS for loop number exception
你做自动化测试为什么总是失败?
SENT协议译码的深入探讨
Matlab superpixels function (2D super pixel over segmentation of image)
[yolov5.yaml parsing]