当前位置:网站首页>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();边栏推荐
- Ping error: unknown name or service
- @Detailed introduction of configuration annotation
- Chisel tutorial - 01 Introduction to Scala
- Tools for debugging makefiles - tool for debugging makefiles
- FFA and ICGA angiography
- [question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
- codeforces每日5题(均1500)-第八天
- 关于CH32库函数与STM32库函数的区别
- Using Google test in QT
- 一份假Offer如何盗走了「Axie infinity」5.4亿美元?
猜你喜欢

About the difference between ch32 library function and STM32 library function

【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏

archery安装测试

QT and OpenGL: load 3D models using the open asset import library (assimp)

UIC564-2 附录4 –阻燃防火测试:火焰的扩散

Set up personal network disk with nextcloud

Aitm3.0005 smoke toxicity test
![[programming problem] [scratch Level 2] December 2019 flying birds](/img/5e/a105f8615f3991635c9ffd3a8e5836.png)
[programming problem] [scratch Level 2] December 2019 flying birds
![[leetcode] 20. Valid brackets](/img/42/5a2c5ec6c1a7dbcdfb2226cdea6a42.png)
[leetcode] 20. Valid brackets

Install sqlserver2019
随机推荐
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
全自动化处理每月缺卡数据,输出缺卡人员信息
At the age of 35, I made a decision to face unemployment
用语雀写文章了,功能真心强大!
archery安装测试
SQL connection problem after downloading (2)
Laser slam learning (2d/3d, partial practice)
c—线性表
BSS 7230 flame retardant performance test of aviation interior materials
Cmake learning notes (1) compile single source programs with cmake
Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
【編程題】【Scratch二級】2019.12 飛翔的小鳥
Preliminary test of optical flow sensor: gl9306
Teach you to make a custom form label by hand
C language greedy snake
Go learning notes (2) basic types and statements (1)
QT creator add custom new file / Project Template Wizard
May day d-light
蓝桥ROS中使用fishros一键安装