当前位置:网站首页>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();
边栏推荐
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- postgres timestamp转人眼时间字符串或者毫秒值
- 蓝桥ROS中使用fishros一键安装
- 光流传感器初步测试:GL9306
- Redis caching tool class, worth owning~
- Tools for debugging makefiles - tool for debugging makefiles
- 【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
- 一份假Offer如何盗走了「Axie infinity」5.4亿美元?
- AITM3.0005 烟雾毒性测试
- Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
猜你喜欢
Les mots ont été écrits, la fonction est vraiment puissante!
【史上最详细】信贷中逾期天数统计说明
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
C - linear table
MP4文件格式解析之结合实例分析
DataGuard active / standby cleanup archive settings
Robomaster visual tutorial (1) camera
FFA and ICGA angiography
FFA与ICGA造影
10 schemes to ensure interface data security
随机推荐
【编程题】【Scratch二级】2019.12 飞翔的小鸟
Go time package common functions
QT creator add JSON based Wizard
SQL uses the in keyword to query multiple fields
BSS 7230 航空内饰材料阻燃性能测试
Using Google test in QT
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
10 schemes to ensure interface data security
面试题详解:用Redis实现分布式锁的血泪史
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
SQL connection problem after downloading (2)
每日刷题记录 (十六)
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
An example analysis of MP4 file format parsing
【史上最详细】信贷中逾期天数统计说明
Automated testing: robot framework is a practical skill that 90% of people want to know
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
Introduction knowledge system of Web front-end engineers