当前位置:网站首页>Linkedblockingqueue source code analysis - initialization
Linkedblockingqueue source code analysis - initialization
2022-07-07 05:13:00 【InfoQ】
LinkedBlockingQueue characteristic
- Blocking queue based on linked list , The underlying data structure is a linked list
- First in first out queue based on linked list , New elements at the end of the team , Get the elements from the team head
- The size of the linked list can be set during initialization , The default is Integer The maximum of
- have access to Iterator To iterate
attribute
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
private static final long serialVersionUID = -6903933977591709194L;
/**
* Linked list node class
*/
static class Node<E> {
E item;
/**
* One of:
* - the real successor Node
* - this Node, meaning the successor is head.next
* - null, meaning there is no successor (this is the last node)
*/
Node<E> next;
Node(E x) { item = x; }
}
/** The capacity bound, or Integer.MAX_VALUE if none */
private final int capacity;
/** Current number of elements */
private final AtomicInteger count = new AtomicInteger();
/**
* Head of linked list.
* Invariant: head.item == null
*/
transient Node<E> head;
/**
* Tail of linked list.
* Invariant: last.next == null
*/
private transient Node<E> last;
/** Lock held by take, poll, etc */
private final ReentrantLock takeLock = new ReentrantLock();
/** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition();
/** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock();
/** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();
......
}
static class Node<E>Node<E> next;private final int capacity;private final AtomicInteger count = new AtomicInteger();transient Node<E> head;private transient Node<E> last;private final ReentrantLock takeLock = new ReentrantLock();private final Condition notEmpty = takeLock.newCondition();private final ReentrantLock putLock = new ReentrantLock();private final Condition notFull = putLock.newCondition();initialization
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
public LinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock putLock = this.putLock;
putLock.lock(); // Never contended, but necessary for visibility
try {
int n = 0;
for (E e : c) {
if (e == null)
throw new NullPointerException();
if (n == capacity)
throw new IllegalStateException("Queue full");
enqueue(new Node<E>(e));
++n;
}
count.set(n);
} finally {
putLock.unlock();
}
}
边栏推荐
- 装饰器基础学习02
- The execution order of return in JS' try catch finally
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- 线程池的创建与使用
- Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
- 一个酷酷的“幽灵”控制台工具
- U++ game learning notes
- 全链路压测:影子库与影子表之争
- AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
- Array initialization of local variables
猜你喜欢

全链路压测:影子库与影子表之争

U++ 游戏类 学习笔记

Error: No named parameter with the name ‘foregroundColor‘

Analysis -- MySQL statement execution process & MySQL architecture

带你遨游银河系的 10 种分布式数据库

Error: No named parameter with the name ‘foregroundColor‘

qt 简单布局 盒子模型 加弹簧

Basic knowledge of road loss of 3GPP channel model

U++ game learning notes

《四》表单
随机推荐
Markdown editor
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
DBSync新增对MongoDB、ES的支持
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
App embedded H5 --- iPhone soft keyboard blocks input text
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
Decorator basic learning 02
01 machine learning related regulations
Understand common network i/o models
【二叉树】二叉树寻路
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
Techniques d'utilisation de sublime
Longest palindrome substring (dynamic programming)
Batch normalization (Standardization) processing
Why is the salary of test and development so high?
JS variable case
Why do many people misunderstand technical debt
精彩速递|腾讯云数据库6月刊
一文搞懂常见的网络I/O模型
谈谈讲清楚这件事的重要性