当前位置:网站首页>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();
边栏推荐
- Download AWS toolkit pycharm
- Teach you to make a custom form label by hand
- Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
- Aitm3.0005 smoke toxicity test
- mysql8.0 ubuntu20.4
- Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
- 10 schemes to ensure interface data security
- Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
- 受限线性表
- 自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
猜你喜欢
Basic learning of SQL Server -- creating databases and tables with the mouse
Introduction to programming hardware
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
Pycharm basic settings latest version 2022
SQL connection problem after downloading (2)
One click free translation of more than 300 pages of PDF documents
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
archery安装测试
保证接口数据安全的10种方案
随机推荐
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
One click installation with fishros in blue bridge ROS
BSS 7230 航空内饰材料阻燃性能测试
AWS AWS help error
C - minute number V3
At the age of 35, I made a decision to face unemployment
Using Google test in QT
SQL uses the in keyword to query multiple fields
Database interview questions + analysis
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
LinkedBlockingQueue源码分析-新增和删除
UIC564-2 附录4 –阻燃防火测试:火焰的扩散
Preliminary test of optical flow sensor: gl9306
Reverse output three digit and arithmetic sequence
每日刷题记录 (十六)
FFA and ICGA angiography
Binary sort tree [BST] - create, find, delete, output
Robomaster visual tutorial (0) Introduction
C language learning
Rock-paper-scissors