当前位置:网站首页>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();
}
}
边栏推荐
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- U++ metadata specifier learning notes
- 2.证券投资基金的概述
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- 一文搞懂常见的网络I/O模型
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- Leetcode(46)——全排列
- 一个酷酷的“幽灵”控制台工具
- vector和类拷贝构造函数
猜你喜欢
Error: No named parameter with the name ‘foregroundColor‘
Leetcode(46)——全排列
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
If you‘re running pod install manually, make sure flutter pub get is executed first.
U++ metadata specifier learning notes
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
批量归一化(标准化)处理
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Auto.js 获取手机所有app名字
Techniques d'utilisation de sublime
随机推荐
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
U++ 游戏类 学习笔记
STM32F103 realize IAP online upgrade application
精彩速递|腾讯云数据库6月刊
JS 的 try catch finally 中 return 的执行顺序
Leetcode(417)——太平洋大西洋水流问题
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
ServiceMesh主要解决的三大痛点
《五》表格
Understand common network i/o models
局部变量的数组初始化问题
Why is the salary of test and development so high?
批量归一化(标准化)处理
How to choose an offer and what factors should be considered
与利润无关的背包问题(深度优先搜索)
使用Thread类和Runnable接口实现多线程的区别
The most complete learning rate adjustment strategy in history LR_ scheduler
如何设计 API 接口,实现统一格式返回?
The sooner you understand the four rules of life, the more blessed you will be