当前位置:网站首页>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();边栏推荐
- One click installation with fishros in blue bridge ROS
- Archery installation test
- The file format and extension of XLS do not match
- Database interview questions + analysis
- SAP HR family member information
- Take you hand in hand to build feign with idea
- Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
- 【leetcode】day1
- HDU - 1260 tickets (linear DP)
- 一鍵免費翻譯300多頁的pdf文檔
猜你喜欢
![Balanced binary tree [AVL tree] - insert, delete](/img/1f/cd38b7c6f00f2b3e85d4560181a9d2.png)
Balanced binary tree [AVL tree] - insert, delete

ping报错:未知的名称或服务

SAP HR social work experience 0023
![Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer](/img/1f/cd38b7c6f00f2b3e85d4560181a9d2.png)
Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer

0-1 knapsack problem

平衡二叉樹【AVL樹】——插入、删除

SAP HR family member information

MP4文件格式解析之结合实例分析

FFA and ICGA angiography

Take you hand in hand to build Eureka server with idea
随机推荐
受限线性表
Aitm3.0005 smoke toxicity test
[summary] some panels and videos seen
Dependency injection
SQL connection problem after downloading (2)
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
postgis学习
May day C - most
SAP HR labor contract information 0016
Database interview questions + analysis
Come on, brother
Binary sort tree [BST] - create, find, delete, output
【leetcode】day1
Jisuan Ke - t3104
SAP HR social work experience 0023
go time包常用函数
串联二极管,提高耐压
网上买基金安全么?
BSS 7230 航空内饰材料阻燃性能测试