当前位置:网站首页>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();
边栏推荐
猜你喜欢
PostGIS learning
Basic learning of SQL Server -- creating databases and tables with the mouse
Preliminary test of optical flow sensor: gl9306
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
The result of innovation in professional courses such as robotics (Automation)
Rectification characteristics of fast recovery diode
光流传感器初步测试:GL9306
UIC564-2 附录4 –阻燃防火测试:火焰的扩散
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
如何衡量产品是否“刚需、高频、痛点”
随机推荐
Robomaster visual tutorial (10) target prediction
【編程題】【Scratch二級】2019.12 飛翔的小鳥
QT and OpenGL: load 3D models using the open asset import library (assimp)
Basic learning of SQL Server -- creating databases and tables with the mouse
Where are you going
Pypharm uses, and the third-party library has errors due to version problems
Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
Use filters to count URL request time
mysql8.0 ubuntu20.4
Rectification characteristics of fast recovery diode
2022.7.7-----leetcode.648
QT creator add custom new file / Project Template Wizard
AITM3.0005 烟雾毒性测试
光流传感器初步测试:GL9306
[leetcode] 20. Valid brackets
Chisel tutorial - 04 Control flow in chisel
limit 与offset的用法(转载)
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
Install sqlserver2019
一鍵免費翻譯300多頁的pdf文檔