/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.common.executor.support;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* A clone of {@linkplain java.util.concurrent.LinkedBlockingQueue}
* with the addition of a {@link #setCapacity(int)} method, allowing us to
* change the capacity of the queue while it is in use.<p>
* <p>
* The documentation for LinkedBlockingQueue follows...<p>
* <p>
* An optionally-bounded {@linkplain BlockingQueue blocking queue} based on
* linked nodes.
* This queue orders elements FIFO (first-in-first-out).
* The <em>head</em> of the queue is that element that has been on the
* queue the longest time.
* The <em>tail</em> of the queue is that element that has been on the
* queue the shortest time. New elements
* are inserted at the tail of the queue, and the queue retrieval
* operations obtain elements at the head of the queue.
* Linked queues typically have higher throughput than array-based queues but
* less predictable performance in most concurrent applications.
*
* <p> The optional capacity bound constructor argument serves as a
* way to prevent excessive queue expansion. The capacity, if unspecified,
* is equal to {@link Integer#MAX_VALUE}. Linked nodes are
* dynamically created upon each insertion unless this would bring the
* queue above capacity.
*
* <p>This class implements all of the <em>optional</em> methods
* of the {@link Collection} and {@link Iterator} interfaces.
*
* <p>This class is a member of the
* <a href="{@docRoot}/../guide/collections/index.html">
* Java Collections Framework</a>.
*
* @param <E> the type of elements held in this collection
* @author Doug Lea
* @since 1.5
**/
public class ResizableCapacityLinkedBlockingQueue<E> extends AbstractQueue<E>
implements
BlockingQueue<E>,
java.io.Serializable {
private static final long serialVersionUID = -6903933977591709194L;
/*
* A variant of the "two lock queue" algorithm. The putLock gates entry to put (and offer), and has an associated condition for waiting puts. Similarly for the takeLock. The "count" field that
* they both rely on is maintained as an atomic to avoid needing to get both locks in most cases. Also, to minimize need for puts to get takeLock and vice-versa, cascading notifies are used. When
* a put notices that it has enabled at least one take, it signals taker. That taker in turn signals others if more items have been entered since the signal. And symmetrically for takes signalling
* puts. Operations such as remove(Object) and iterators acquire both locks.
*/
/**
* Linked list node class
*/
static class Node<E> {
/**
* The item, volatile to ensure barrier separating write and read
*/
volatile E item;
Node<E> next;
Node(E x) {
item = x;
}
}
/**
* The capacity bound, or Integer.MAX_VALUE if none
*/
private int capacity;
/**
* Current number of elements
*/
private final AtomicInteger count = new AtomicInteger(0);
/**
* Head of linked list
*/
private transient Node<E> head;
/**
* Tail of linked list
*/
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();
/**
* Signal a waiting take. Called only from put/offer (which do not
* otherwise ordinarily lock takeLock.)
*/
private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
notEmpty.signal();
} finally {
takeLock.unlock();
}
}
/**
* Signal a waiting put. Called only from take/poll.
*/
private void signalNotFull() {
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
notFull.signal();
} finally {
putLock.unlock();
}
}
/**
* Create a node and link it at end of queue
*
* @param x the item
*/
private void insert(E x) {
last = last.next = new Node<E>(x);
}
/**
* Remove a node from head of queue,
*
* @return the node
*/
private E extract() {
Node<E> first = head.next;
head = first;
E x = first.item;
first.item = null;
return x;
}
/**
* Lock to prevent both puts and takes.
*/
private void fullyLock() {
putLock.lock();
takeLock.lock();
}
/**
* Unlock to allow both puts and takes.
*/
private void fullyUnlock() {
takeLock.unlock();
putLock.unlock();
}
/**
* Creates a <tt>LinkedBlockingQueue</tt> with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public ResizableCapacityLinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
/**
* Creates a <tt>LinkedBlockingQueue</tt> with the given (fixed) capacity.
*
* @param capacity the capacity of this queue.
* @throws IllegalArgumentException if <tt>capacity</tt> is not greater
* than zero.
*/
public ResizableCapacityLinkedBlockingQueue(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException();
}
this.capacity = capacity;
last = head = new Node<E>(null);
}
/**
* Creates a <tt>LinkedBlockingQueue</tt> with a capacity of
* {@link Integer#MAX_VALUE}, initially containing the elements of the
* given collection,
* added in traversal order of the collection's iterator.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if <tt>c</tt> or any element within it
* is <tt>null</tt>
*/
public ResizableCapacityLinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
for (Iterator<? extends E> it = c.iterator(); it.hasNext();) {
add(it.next());
}
}
// this doc comment is overridden to remove the reference to collections
// greater in size than Integer.MAX_VALUE
/**
* Returns the number of elements in this queue.
*
* @return the number of elements in this queue.
*/
@Override
public int size() {
return count.get();
}
/**
* Set a new capacity for the queue. Increasing the capacity can
* cau
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
hippo4j-C/C++项目开发资源 (1874个子文件)
lightinthebox.avif 3KB
cn.hippo4j.agent.core.boot.BootService 858B
cn.hippo4j.agent.core.boot.BootService 844B
cn.hippo4j.core.api.ClientNetworkService 60B
cn.hippo4j.core.api.ClientNetworkService 60B
startup.cmd 1KB
shutdown.cmd 343B
agent.config 4KB
lombok.config 905B
index.css 414KB
app.6f535c21.css 251KB
custom.css 16KB
chunk-5911c282.13a7e89e.css 5KB
chunk-libs.3dfb7769.css 3KB
chunk-38e7e764.4353ab43.css 3KB
chunk-0fde2e8e.09d1406a.css 2KB
chunk-eee8a83e.a09ed6a0.css 1KB
chunk-02066c0e.20d79963.css 856B
waves.css 825B
chunk-296c90bf.95ae0d9d.css 763B
chunk-d9fc0e72.95ae0d9d.css 763B
chunk-37b6768d.54599d3b.css 763B
chunk-05d50b2c.95ae0d9d.css 763B
chunk-19132c4b.95ae0d9d.css 763B
chunk-2e217faa.95ae0d9d.css 763B
chunk-4413401e.65cac33b.css 749B
chunk-ef888edc.5f8941eb.css 745B
chunk-d6c1d344.35874984.css 631B
chunk-a89383d2.35874984.css 631B
chunk-078a7535.35874984.css 631B
chunk-5428753b.35874984.css 631B
chunk-4f40863a.35874984.css 631B
chunk-60c39f89.35874984.css 631B
chunk-adca2a60.35874984.css 631B
chunk-149a43cf.35874984.css 631B
chunk-1b3cdbc8.35874984.css 631B
chunk-3a6f2dc9.35874984.css 631B
index.module.css 365B
styles.module.css 138B
cn.hippo4j.common.executor.support.CustomBlockingQueue 40B
cn.hippo4j.common.executor.support.CustomRejectedExecutionHandler 64B
hippo4j-plugin.def 888B
hippo4j-plugin.def 888B
hippo4j-plugin.def 880B
hippo4j-plugin.def 858B
.env.development 565B
Dockerfile 2KB
.editorconfig 244B
.eslintignore 46B
.eslintignore 40B
spring.factories 372B
spring.factories 243B
spring.factories 229B
spring.factories 179B
spring.factories 179B
spring.factories 153B
spring.factories 152B
spring.factories 147B
spring.factories 144B
spring.factories 143B
spring.factories 143B
spring.factories 140B
spring.factories 138B
spring.factories 137B
spring.factories 137B
spring.factories 129B
spring.factories 123B
spring.factories 114B
401.gif 160KB
401.089007e7.gif 160KB
hippo4j.gif 6KB
hippo4j.gif 6KB
hippo4j.ecba1844.gif 6KB
.gitignore 657B
.gitignore 312B
.gitignore 258B
index.html 5KB
index.html 2KB
index.html 488B
favicon.ico 4KB
favicon.ico 3KB
favicon.ico 3KB
favicon.ico 3KB
hippo4j_favicon.ico 3KB
cn.hippo4j.core.extension.spi.IOldSpi 42B
maven-wrapper.jar 0B
ResizableCapacityLinkedBlockingQueue.java 25KB
DefaultThreadPoolPluginManager.java 17KB
DynamicThreadPoolRefreshListener.java 17KB
ConfigServiceImpl.java 16KB
BootstrapInstrumentBoost.java 15KB
DynamicThreadPoolPostProcessor.java 15KB
ThreadPoolBuilder.java 14KB
ExtensibleThreadPoolExecutor.java 13KB
ClientWorker.java 13KB
LongPollingService.java 13KB
DynamicThreadPoolAutoConfiguration.java 13KB
ClassEnhancePluginDefine.java 12KB
HttpUtil.java 12KB
BeforeCheckConfiguration.java 12KB
共 1874 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
资源评论
xyq2024
- 粉丝: 2465
- 资源: 5460
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- OpenEuler22.03TLS-SP3系统ssh漏洞官方升级包
- Jmeter实现同一线程组内接口并行执行
- MySQL的安装与配置PDF
- python007-django疫情数据可视化分析系统(LW+PPT).zip
- python006-django基于python技术的学生管理系统的设计与开发.zip
- python005-基于Python爬虫的网络小说数据分析系统的设计与实现.zip
- vs2015 udp 广播 demo
- 创维42L20HW(8DA6)软件数据.rar
- gcc15交叉编译工具链windows版,用于编译龙芯应用,gcc version 15.0.0 20241119 (experimental) (GCC)
- python004-基于python的抑郁症患者看护系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功