当前位置:网站首页>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();边栏推荐
- Ping error: unknown name or service
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- 【路径规划】使用垂距限值法与贝塞尔优化A星路径
- Ora-01741 and ora-01704
- 10 schemes to ensure interface data security
- ASP. Net open web page
- MySQL Architecture
- HB 5469民用飞机机舱内部非金属材料燃烧试验方法
- Postgres timestamp to human eye time string or millisecond value
- P1055 [noip2008 popularization group] ISBN number
猜你喜欢

Aitm3.0005 smoke toxicity test

串联二极管,提高耐压

The file format and extension of XLS do not match

二叉排序树【BST】——创建、查找、删除、输出

Take you hand in hand to build Eureka server with idea

FFA与ICGA造影

Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module

【实验分享】通过Console口登录到Cisco设备

Wechat applet development beginner 1
![[leetcode] 20. Valid brackets](/img/42/5a2c5ec6c1a7dbcdfb2226cdea6a42.png)
[leetcode] 20. Valid brackets
随机推荐
Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
HB 5469 combustion test method for non-metallic materials in civil aircraft cabin
Ora-02437 failed to verify the primary key violation
The file format and extension of XLS do not match
Idea automatically generates serialVersionUID
ASP. Net query implementation
用语雀写文章了,功能真心强大!
Take you hand in hand to build feign with idea
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
Extract the file name under the folder under win
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
ASP. Net core middleware request processing pipeline
[experiment sharing] log in to Cisco devices through the console port
C语言学习
P2141 [noip2014 popularization group] abacus mental arithmetic test
自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
c—线性表
Restricted linear table
通达信买基金安全吗?