当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

U++4 接口 学习笔记

If you‘re running pod install manually, make sure flutter pub get is executed first.

Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)

01 machine learning related regulations

拿到PMP认证带来什么改变?

pmp真的有用吗?

【愚公系列】2022年7月 Go教学课程 005-变量

How to design API interface and realize unified format return?

AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘

【问道】编译原理
随机推荐
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
App embedded H5 --- iPhone soft keyboard blocks input text
PMP证书有没有必要续期?
CentOS 7.9安装Oracle 21c历险记
Is PMP really useful?
创始人负债10亿,开课吧即将“下课”?
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
QT控件样式系列(一)之QSlider
c语言神经网络基本代码大全及其含义
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
基于Bevy游戏引擎和FPGA的双人游戏
【opencv】图像形态学操作-opencv标记不同连通域的位置
Longest non descent subsequence (LIS) (dynamic programming)
Why is the salary of test and development so high?
2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
如何设计 API 接口,实现统一格式返回?
Window scheduled tasks