当前位置:网站首页>LinkedBlockingQueue源码分析-新增和删除
LinkedBlockingQueue源码分析-新增和删除
2022-07-07 21:54:00 【InfoQ】
LinkedBlockingQueue源码分析-新增和删除
新增
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
while (count.get() == capacity) {
notFull.await();
}
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
}
private void enqueue(Node<E> node) {
last = last.next = node;
}
if (e == null) throw new NullPointerException();
int c = -1;
while (count.get() == capacity) { }
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
if (c == 0) signalNotEmpty();
删除
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
private E dequeue() {
Node<E> h = head;
Node<E> first = h.next;
h.next = h; // help GC
head = first;
E x = first.item;
first.item = null;
return x;
}
public E peek() {
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
Node<E> first = head.next;
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}
private E dequeue() {
Node<E> h = head;
Node<E> first = h.next;
h.next = h; // help GC
head = first;
E x = first.item;
first.item = null;
return x;
}
int c = -1;
final AtomicInteger count = this.count;
while (count.get() == 0) { notEmpty.await(); }
x = dequeue();
if (c > 1) notEmpty.signal();
if (c == capacity) signalNotFull();
边栏推荐
- Orthodontic precautions (continuously updated)
- FFA and ICGA angiography
- Take you hand in hand to build feign with idea
- One click installation with fishros in blue bridge ROS
- SQL uses the in keyword to query multiple fields
- Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
- At the age of 35, I made a decision to face unemployment
- About the difference between ch32 library function and STM32 library function
- 【路径规划】使用垂距限值法与贝塞尔优化A星路径
- [leetcode] 20. Valid brackets
猜你喜欢
Take you hand in hand to build Eureka server with idea
Class C design questions
[experiment sharing] log in to Cisco devices through the console port
用语雀写文章了,功能真心强大!
Pycharm basic settings latest version 2022
Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
Ping error: unknown name or service
蓝桥ROS中使用fishros一键安装
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
Rectification characteristics of fast recovery diode
随机推荐
解析token的网址
Possible SQL for Oracle table lookup information
【leetcode】day1
Come on, brother
postgis学习
go time包常用函数
C simple question 2
网上买基金安全么?
BSS 7230 flame retardant performance test of aviation interior materials
Enumeration, simulation, and sorting
一鍵免費翻譯300多頁的pdf文檔
Ora-01741 and ora-01704
C method question 2
Download AWS toolkit pycharm
Take you hand in hand to build Eureka server with idea
Flash download setup
【7.4】25. Turn over the linked list in groups of K
SAP HR reward and punishment information export
Orthodontic precautions (continuously updated)
Dependency injection