当前位置:网站首页>LinkedBlockingQueue源码分析-新增和删除
LinkedBlockingQueue源码分析-新增和删除
2022-07-07 21:54:00 【InfoQ】
LinkedBlockingQueue源码分析-新增和删除
新增
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();
删除
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();
边栏推荐
- Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
- Data Lake (XV): spark and iceberg integrate write operations
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- postgis学习
- Balanced binary tree [AVL tree] - insert, delete
- [summary] some panels and videos seen
- One click installation with fishros in blue bridge ROS
- 数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
- One click free translation of more than 300 pages of PDF documents
- @Configuration注解的详细介绍
猜你喜欢
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
Idea automatically generates serialVersionUID
蓝桥ROS中使用fishros一键安装
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
0-1 knapsack problem
How did a fake offer steal $540million from "axie infinity"?
FFA and ICGA angiography
The file format and extension of XLS do not match
ASP. Net core middleware request processing pipeline
Ping error: unknown name or service
随机推荐
Slam interview summary
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
P5594 [xr-4] simulation match
Traduction gratuite en un clic de plus de 300 pages de documents PDF
神奇快速幂
Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
【路径规划】使用垂距限值法与贝塞尔优化A星路径
一键免费翻译300多页的pdf文档
gorm 关联关系小结
Gorm Association summary
Resolve the URL of token
【LeetCode】20、有效的括号
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
SQL 使用in关键字查询多个字段
Dependency injection
【leetcode】day1
35岁那年,我做了一个面临失业的决定
C - linear table
HDU - 1260 Tickets(线性DP)
Rectification characteristics of fast recovery diode