当前位置:网站首页>Linkedblockingqueue source code analysis - add and delete
Linkedblockingqueue source code analysis - add and delete
2022-07-07 23:58:00 【InfoQ】
LinkedBlockingQueue Source code analysis - Add and delete
newly added
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
while (count.get() == capacity) {
notFull.await();
}
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
}
private void enqueue(Node<E> node) {
last = last.next = node;
}
if (e == null) throw new NullPointerException();
int c = -1;
while (count.get() == capacity) { }
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
if (c == 0) signalNotEmpty();
Delete
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
private E dequeue() {
Node<E> h = head;
Node<E> first = h.next;
h.next = h; // help GC
head = first;
E x = first.item;
first.item = null;
return x;
}
public E peek() {
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
Node<E> first = head.next;
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}
private E dequeue() {
Node<E> h = head;
Node<E> first = h.next;
h.next = h; // help GC
head = first;
E x = first.item;
first.item = null;
return x;
}
int c = -1;
final AtomicInteger count = this.count;
while (count.get() == 0) { notEmpty.await(); }
x = dequeue();
if (c > 1) notEmpty.signal();
if (c == capacity) signalNotFull();
边栏推荐
- gorm 关联关系小结
- Chisel tutorial - 01 Introduction to Scala
- LinkedBlockingQueue源码分析-新增和删除
- codeforces每日5题(均1500)-第八天
- UIC564-2 附录4 –阻燃防火测试:火焰的扩散
- 【编程题】【Scratch二级】2019.12 飞翔的小鸟
- P1308 [noip2011 popularity group] count the number of words
- HDU - 1260 tickets (linear DP)
- Set up personal network disk with nextcloud
- Using Google test in QT
猜你喜欢
Archery installation test
The result of innovation in professional courses such as robotics (Automation)
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
Flash download setup
archery安装测试
Traduction gratuite en un clic de plus de 300 pages de documents PDF
Opengl3.3 mouse picking up objects
10 schemes to ensure interface data security
DataGuard active / standby cleanup archive settings
随机推荐
每日刷题记录 (十六)
Is it safe to buy funds online?
[programming problem] [scratch Level 2] December 2019 flying birds
Learn about scratch
Laser slam learning (2d/3d, partial practice)
Pigsty:开箱即用的数据库发行版
Codeworks 5 questions per day (average 1500) - day 8
QT and OpenGL: load 3D models using the open asset import library (assimp)
Chisel tutorial - 04 Control flow in chisel
AWS AWS help error
关于组织2021-2022全国青少年电子信息智能创新大赛西南赛区(四川)复赛的通知
Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
Robomaster visual tutorial (11) summary
FFA and ICGA angiography
BSS 7230 航空内饰材料阻燃性能测试
Reverse output three digit and arithmetic sequence
HB 5469 combustion test method for non-metallic materials in civil aircraft cabin
机器人(自动化)等专业课程创新的结果
Data Lake (XV): spark and iceberg integrate write operations
【推荐系统基础】正负样本采样和构造