当前位置:网站首页>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();边栏推荐
猜你喜欢

Aitm3.0005 smoke toxicity test

SAP HR family member information

SAP HR labor contract information 0016

串联二极管,提高耐压
![[leetcode] 20. Valid brackets](/img/42/5a2c5ec6c1a7dbcdfb2226cdea6a42.png)
[leetcode] 20. Valid brackets

Take you hand in hand to build feign with idea

95. (cesium chapter) cesium dynamic monomer-3d building (building)
![P1067 [noip2009 popularity group] polynomial output (difficult, pit)](/img/1f/a798879a0d65eccefa339b288f2102.jpg)
P1067 [noip2009 popularity group] polynomial output (difficult, pit)

快速回复二极管整流特性

Problems faced when connecting to sqlserver after downloading (I)
随机推荐
P5594 [xr-4] simulation match
Rectification characteristics of fast recovery diode
Possible SQL for Oracle table lookup information
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
【LeetCode】20、有效的括号
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
FFA与ICGA造影
ping报错:未知的名称或服务
蓝桥ROS中使用fishros一键安装
【汇总】看过的一些Panel与视频
SAP HR labor contract information 0016
Wechat applet development beginner 1
【leetcode】day1
SQL uses the in keyword to query multiple fields
Take you hand in hand to build Eureka server with idea
网上买基金安全么?
Go time package common functions
Extract the file name under the folder under win
二叉排序树【BST】——创建、查找、删除、输出
Data Lake (XV): spark and iceberg integrate write operations