当前位置:网站首页>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 !
边栏推荐
- Redis master-slave mode
- 16 channel water lamp experiment based on Proteus (assembly language)
- [loss functions of L1, L2 and smooth L1]
- Seven polymorphisms
- Codeforces Round #804 (Div. 2)
- Complete activity switching according to sliding
- Understanding the architecture type of mobile CPU
- Matlab boundarymask function (find the boundary of the divided area)
- Codeworks 5 questions per day (1700 average) - day 5
- Master the new features of fluent 2.10
猜你喜欢
Riddle 1
MySQL index - extended data
Understand kotlin from the perspective of an architect
Reading notes of growth hacker
Embedded software architecture design - message interaction
Splunk configuration 163 mailbox alarm
Flutter2 heavy release supports web and desktop applications
无线WIFI学习型8路发射遥控模块
Tabbar configuration at the bottom of wechat applet
abap查表程序
随机推荐
byte2String、string2Byte
MySQL data table operation DDL & data type
想问问,如何选择券商?在线开户是很安全么?
Flutter2 heavy release supports web and desktop applications
How to recover the information server and how to recover the server data [easy to understand]
Understanding the architecture type of mobile CPU
Understand kotlin from the perspective of an architect
JS for loop number exception
Read and understand the rendering mechanism and principle of flutter's three trees
[deploy pytoch project through onnx using tensorrt]
Time tools
How can beginners learn flutter efficiently?
Video networkstate property
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
What is the difference between canvas and SVG?
Riddle 1
MySQL storage engine
The evolution of mobile cross platform technology
MySQL index (1)