当前位置:网站首页>Linkedblockingqueue source code analysis - add and delete
Linkedblockingqueue source code analysis - add and delete
2022-07-07 23:58:00 【InfoQ】
LinkedBlockingQueue Source code analysis - Add and delete
newly added
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();Delete
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();边栏推荐
猜你喜欢

35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了

【编程题】【Scratch二级】2019.12 飞翔的小鸟

FFA and ICGA angiography

快速回复二极管整流特性

SQL connection problem after downloading (2)

Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis

The function is really powerful!

神奇快速幂

archery安装测试

每日刷题记录 (十六)
随机推荐
Laser slam learning (2d/3d, partial practice)
10 schemes to ensure interface data security
HDU - 1260 Tickets(线性DP)
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
Robomaster visual tutorial (0) Introduction
How did a fake offer steal $540million from "axie infinity"?
Kubectl's handy command line tool: Oh my Zsh tips and tricks
2022.7.7-----leetcode.648
机器人(自动化)等专业课程创新的结果
Pigsty:开箱即用的数据库发行版
[question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
Solutions to problems in sqlserver deleting data in tables
Automated testing: robot framework is a practical skill that 90% of people want to know
Set up personal network disk with nextcloud
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
Robomaster visual tutorial (1) camera
自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
P1055 [noip2008 popularization group] ISBN number
DataGuard active / standby cleanup archive settings