当前位置:网站首页>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();
边栏推荐
- Is it safe for tongdaxin to buy funds?
- Oracle string sorting
- Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
- 自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
- 平衡二叉树【AVL树】——插入、删除
- webflux - webclient Connect reset by peer Error
- 企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
- 【LeetCode】20、有效的括号
- Ora-02437 failed to verify the primary key violation
- Apng2gif solutions to various problems
猜你喜欢
Ping error: unknown name or service
神奇快速幂
Take you hand in hand to build feign with idea
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
【路径规划】使用垂距限值法与贝塞尔优化A星路径
C method question 1
SQL connection problem after downloading (2)
Archery installation test
AITM3.0005 烟雾毒性测试
一鍵免費翻譯300多頁的pdf文檔
随机推荐
平衡二叉树【AVL树】——插入、删除
光流传感器初步测试:GL9306
Wechat applet development beginner 1
An example analysis of MP4 file format parsing
SQL 使用in关键字查询多个字段
【路径规划】使用垂距限值法与贝塞尔优化A星路径
Chisel tutorial - 04 Control flow in chisel
Chisel tutorial - 01 Introduction to Scala
正畸注意事项(持续更新中)
Balanced binary tree [AVL tree] - insert, delete
postgis学习
通达信买基金安全吗?
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
【汇总】看过的一些Panel与视频
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
The file format and extension of XLS do not match
postgres timestamp转人眼时间字符串或者毫秒值
DataGuard active / standby cleanup archive settings
aws-aws help报错
平衡二叉樹【AVL樹】——插入、删除