当前位置:网站首页>LinkedBlockingQueue源码分析-初始化
LinkedBlockingQueue源码分析-初始化
2022-07-06 23:13:00 【InfoQ】
LinkedBlockingQueue特点
- 基于链表实现的阻塞队列,底层的数据结构为链表
- 基于链表实现先入先出的队列,新元素放在队尾,从队头部获取元素
- 链表大小在初始化的时候可以设置大小,默认为 Integer 的最大值
- 可以使用Iterator 进行迭代
属性
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();初始化
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();
}
}
边栏推荐
- Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
- Leetcode longest public prefix
- 腾讯云数据库公有云市场稳居TOP 2!
- 【PHP SPL笔记】
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- Factor analysis r practice (with R installation tutorial and code)
- JS input and output
- JS also exports Excel
- 【QT】自定义控件-Loading
- Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
猜你喜欢
随机推荐
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
U++ game learning notes
第一篇论文的写作流程
sublime使用技巧
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
Using thread class and runnable interface to realize the difference between multithreading
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
np.random.shuffle与np.swapaxis或transpose一起时要慎用
3. Type of fund
全链路压测:影子库与影子表之争
精彩速递|腾讯云数据库6月刊
When knative meets webassembly
SQL injection HTTP header injection
STM32F103 realize IAP online upgrade application
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
01机器学习相关规定
A line of R code draws the population pyramid
JDBC link Oracle reference code
DBSync新增对MongoDB、ES的支持
【QT】自定义控件-Loading







![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
