当前位置:网站首页>Source code learning: atomicinteger class code internal logic
Source code learning: atomicinteger class code internal logic
2022-06-26 13:00:00 【Learning notes of net fish】
1. AtomicInteger What is it?
An atom renewable int value . A description of the properties of atomic variables , see also java.util.concurrent.atomic Package specification .AtomicInteger For applications such as atomic up counters , Can not be used as Integer substitute . however , This class does extend Number, To allow uniform access to tools and utilities that handle number based classes .
2. Set to use CAS updated
setUp to use Unsafe.compareAndAwapInt for update
3. Calculate the offset
Check the fields declared public by the user permission acquisition , Then, according to the fields with permission , Get the offset of the system value Get one long The value of the offset of type
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) {
throw new Error(ex); }
}
4. Lazy assignment
/** * Eventually sets to the given value. At last, the assignment operation is performed * * @param newValue the new value * @since 1.6 */
public final void lazySet(int newValue) {
unsafe.putOrderedInt(this, valueOffset, newValue);
}
5. getAndSet Get first and then assign ( involve CAS)
public final int getAndSetInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var4));
return var5;
}
First step : Pass in a value to replace var5; Then calculate according to var1 and war2 adopt getIntVolatile To calculate the var5
The second step : If compareAndSwapInt Comparison results and var5 equal , return var5
6. compareAndSet
/** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * @param expect the expected value * @param update the new value * @return {@code true} if successful. False return indicates that * the actual value was not equal to the expected value. */
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
If the actual value to be updated and the expected value to be updated do not want to wait, return false, If the actual value to be updated is equal to the expected value, return true
7. Get and update values
First step :
/** * Atomically adds the given value to the current value. * * @param delta the value to add * @return the updated value */
public final int addAndGet(int delta) {
return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
}
The second step :
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
If the original value var1 And offset var2 The resulting calculations and results var5 equal , to var5 Add the corresponding increment delta namely var4
8.getAndUpdate
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev, next;
do {
prev = get();
next = updateFunction.applyAsInt(prev);
} while (!compareAndSet(prev, next));
return prev;
}
Update the current value atomically with the result of applying a given function , Return the value before updating . This function should have no side effects , Because when an attempt to update fails due to thread contention , Will try the update again .
9.updateAndGet
public final int updateAndGet(IntUnaryOperator updateFunction) {
int prev, next;
do {
prev = get();
next = updateFunction.applyAsInt(prev);
} while (!compareAndSet(prev, next));
return next;
}
Update the current value atomically with the result of applying a given function , Returns the updated value . This function should have no side effects , Because when an attempt to update fails due to thread contention , Will try the update again .
10.getAndAccumulate
public final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
int prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsInt(prev, x);
} while (!compareAndSet(prev, next));
return prev;
}
Update the current value atomically with the result of applying the given function to the current value and the given value , Return the value before update . This function should have no side effects , Because when an attempt to update fails due to thread contention , The function may be reapplied . The first argument to the function is the current value , The second parameter is the given update .
11. accumulateAndGet
public final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
int prev, next;
do {
prev = get();
next = accumulatorFunction.applyAsInt(prev, x);
} while (!compareAndSet(prev, next));
return next;
}
Update the current value atomically with the result of applying the given function to the current value and the given value , Returns the updated value . This function should have no side effects , Because when an attempt to update fails due to thread contention , The function may be reapplied . The first argument to the function is the current value , The second parameter is the given update .
边栏推荐
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- 一个初级多线程服务器模型
- KITTI Tracking dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
- 7-3 最低通行费
- C# const详解:C#常量的定义和使用
- 数字信号处理——线性相位型(Ⅰ、Ⅲ型)FIR滤波器设计(1)
- PHP get directory size
- Installing MySQL under Linux (RPM package installation)
- 班主任让开股票账户,在挖财理财开户安全吗?
- 使用SSH密钥对登陆服务器
猜你喜欢

Installing MySQL under Linux (RPM package installation)

【网络是怎么连接的】第二章(下):一个网络包的接收

Goto statement to realize shutdown applet

processing 函数translate(mouseX, mouseY)学习

Laravel subdomain accesses different routing files and different modules
RSS rendering of solo blog system failed

深入解析 MySQL binlog

How does easygbs solve the abnormal use of intercom function?

详细讲解C语言10(C语言系列)

Deeply analyze the differences between dangbei box B3, Tencent Aurora 5S and Xiaomi box 4S
随机推荐
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
Summary of some application research cases of UAV Remote Sensing in forest monitoring
C# const详解:C#常量的定义和使用
别乱用 FULL_CASE 和 PARALLEL_CASE
心脏滴血漏洞(CVE-2014-0160)分析与防护
KITTI Detection dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
goto语句实现关机小程序
小白懒人专用-win10-win11一键安装版
Research and development practice of Kwai real-time data warehouse support system
程序员必备,一款让你提高工作效率N倍的神器uTools
机组实践实验8——使用CMStudio设计基于基本模型机微程序指令(1)
做自媒体视频的各种常用工具合集奉上
sqlalchemy event listen Automatic generate CRUD excel
计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
使用SSH密钥对登陆服务器
el-form-item 包含两个input, 校验这两个input
RSS rendering of solo blog system failed
自动化测试的局限性你知道吗?
栈,后入先出
手把手带你学会Odoo OWL组件开发(7):OWL项目实战使用