当前位置:网站首页>juc-Queue接口以及其实现类
juc-Queue接口以及其实现类
2022-04-21 19:38:00 【且听風吟丶】
Queue


BlockingQueue



ArrayBlockingQueue

构造函数
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
public ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c) {
this(capacity, fair);
final ReentrantLock lock = this.lock;
lock.lock(); // Lock only for visibility, not mutual exclusion
try {
int i = 0;
try {
for (E e : c) {
checkNotNull(e);
items[i++] = e;
}
} catch (ArrayIndexOutOfBoundsException ex) {
throw new IllegalArgumentException();
}
count = i;
putIndex = (i == capacity) ? 0 : i;
} finally {
lock.unlock();
}
}
LinkedBlockingQueue

构造函数
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();
}
}
PriorityBlockingQueue

构造函数
public PriorityBlockingQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
public PriorityBlockingQueue(Collection<? extends E> c) {
this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
boolean heapify = true; // true if not known to be in heap order
boolean screen = true; // true if must screen for nulls
if (c instanceof SortedSet<?>) {
SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
this.comparator = (Comparator<? super E>) ss.comparator();
heapify = false;
}
else if (c instanceof PriorityBlockingQueue<?>) {
PriorityBlockingQueue<? extends E> pq =
(PriorityBlockingQueue<? extends E>) c;
this.comparator = (Comparator<? super E>) pq.comparator();
screen = false;
if (pq.getClass() == PriorityBlockingQueue.class) // exact match
heapify = false;
}
Object[] a = c.toArray();
int n = a.length;
// If c.toArray incorrectly doesn't return Object[], copy it.
if (a.getClass() != Object[].class)
a = Arrays.copyOf(a, n, Object[].class);
if (screen && (n == 1 || this.comparator != null)) {
for (int i = 0; i < n; ++i)
if (a[i] == null)
throw new NullPointerException();
}
this.queue = a;
this.size = n;
if (heapify)
heapify();
}
public PriorityBlockingQueue(int initialCapacity) {
this(initialCapacity, null);
}
public PriorityBlockingQueue(int initialCapacity,
Comparator<? super E> comparator) {
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
this.comparator = comparator;
this.queue = new Object[initialCapacity];
}
SynchronousQueue

public SynchronousQueue() {
this(false);
}
public SynchronousQueue(boolean fair) {
transferer = fair ? new TransferQueue<E>() : new TransferStack<E>();
}
DelayQueue

public DelayQueue() {
}
public DelayQueue(Collection<? extends E> c) {
this.addAll(c);
}
LinkedTransferQueue

public LinkedTransferQueue() {
}
public LinkedTransferQueue(Collection<? extends E> c) {
this();
addAll(c);
}
版权声明
本文为[且听風吟丶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_37151886/article/details/124320609
边栏推荐
- Low density lipoprotein research - LDL-C content detection kit scheme
- 华为开发者创新中心教你一分钟了解HarmonyOS
- 669. Pruning binary search tree
- 如何让Join跑的更快?(文末送书)
- Openkg open source series | Encyclopedia of characters knowledge map (Southeast University)
- Learn MySQL performance tuning and make your database smooth
- detectron2 中yacs的使用解读
- Mysql database index interview questions (latest version)
- LeetCode 山羊拉丁文[模拟 字符串] HERODING的LeetCode之路
- 使用8266做串口调试工具二
猜你喜欢

Seata (I) service configuration and startup

Use the mremoting tool to manage all remote connections

Some keywords of the robotframework cannot be used or are black

【网络协议】ip addr

Mysql database index interview questions (latest version)

Low density lipoprotein research - LDL-C content detection kit scheme

Installing MySQL 8 on Linux centos7 (simple and practical)

数据分析之数据预处理

“最难就业季“中的大学生就业:本硕过半有着落 高职生成香饽饽
![[advanced C language] ⑥ detailed explanation of function pointer](/img/30/729fe5d804ac1a423397ada599d767.png)
[advanced C language] ⑥ detailed explanation of function pointer
随机推荐
Chekine series high density lipoprotein (HDL-C) content detection scheme
MySQL MHA high availability cluster deployment and failover
Abbexa MPO (FITC) / CD3 (PE) combined antibody
静态链接与动态链接
Redis installation and configuration startup
使用CMake构建/在命令行上构建项目
配置PyTorch、TensorFlow 环境
Which futures trading platform is the safest? How do I choose?
iMeta | EndNote调整完美引文格式教程(视频)
What are the factors affecting VPS website optimization?
[network protocol] IP addr
Comparison of F (ab ') 2 IgG isotype in abbexa goat
Using MCU Xpress to develop rt1060 (1) -- drive RGB interface LCD
【JZ47 礼物的最大价值】
669. Pruning binary search tree
“最难就业季“中的大学生就业:本硕过半有着落 高职生成香饽饽
每日CISSP认证常错题(2022年4月20日)
PHP + redis rush buying spike
MySQL throughput
OpenHarmony Sensor 模块Callback注册和回调全流程