当前位置:网站首页>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();
边栏推荐
- Laser slam learning (2d/3d, partial practice)
- 企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
- P1308 [noip2011 popularity group] count the number of words
- 正畸注意事项(持续更新中)
- [summary] some panels and videos seen
- Balanced binary tree [AVL tree] - insert, delete
- Codeworks 5 questions per day (average 1500) - day 8
- How did a fake offer steal $540million from "axie infinity"?
- SQL uses the in keyword to query multiple fields
- 【leetcode】day1
猜你喜欢
Wechat applet development beginner 1
Traduction gratuite en un clic de plus de 300 pages de documents PDF
Navicat connects Oracle
PostGIS learning
Svn relocation
Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
神奇快速幂
Flash download setup
MongoDB快速入门
SQL connection problem after downloading (2)
随机推荐
Chisel tutorial - 04 Control flow in chisel
AITM3.0005 烟雾毒性测试
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
About the difference between ch32 library function and STM32 library function
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
Oracle statistics by time
MP4文件格式解析之结合实例分析
One of the anti climbing methods
Rock-paper-scissors
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
SAP HR labor contract information 0016
Rectification characteristics of fast recovery diode
mysql8.0 ubuntu20.4
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
10 schemes to ensure interface data security
Anti climbing means cracking the second
FFA与ICGA造影
redis缓存工具类,值得拥有~
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
archery安装测试