当前位置:网站首页>Atomicinteger atomic operation class
Atomicinteger atomic operation class
2022-06-11 07:04:00 【I will definitely go to Maidang in rainbow sea】
️ Personal website :code treasure , Welcome to visit
My public number :code treasure , Share your learning resources , Welcome to your attention
Thank you very much for your support and praise
Atomicity
One or more operations Either all of them are executed and the execution process will not be interrupted by any factors , Or they don't do it .
int++ It's not an atomic operation , So when a thread reads its value and adds 1 when , Another thread may modify the original int value , This will cause an error .
Atomic classes
java.util.concurrent This package provides a set of atomic classes . Its basic characteristic is in multithreading environment , When there are multiple threads executing the methods contained in the instances of these classes at the same time , Be exclusive , That is, when a thread enters a method , When executing the instructions , Will not be interrupted by other threads , And other threads are like spinlocks , Wait until the method execution is complete , Then from JVM Select a thread from the waiting queue to enter , It's just a logical understanding .
Atomic classes :AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
An array of atoms :AtomicIntegerArray,AtomicLongArray,AtomicReferenceArray
Atomic property updater :AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
solve ABA The atomic class of the problem :AtomicMarkableReference( By introducing a boolean To reflect whether there has been any change in the middle ),AtomicStampedReference( By introducing a int Come to lega to reflect whether there has been any change in the middle )
AtomicInteger principle
public class AtomicInteger extends Number implements java.io.Serializable {
private static final long serialVersionUID = 6214790243416807050L;
// setup to use Unsafe.compareAndSwapInt for updates
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) {
throw new Error(ex); }
}
private volatile int value;
}
AtomicInteger Class mainly uses CAS (compare and swap) + volatile and native Method to ensure atomic operation , To avoid synchronized The high cost of , Greatly improved execution efficiency .
CAS The principle is to compare the expected value with the original value , If it is the same, update it to a new value .
UnSafe Class objectFieldOffset() A method is a local method , This method is used to get value The memory address of the property , The return value is valueOffset, We can go through this valueOffset visit value attribute .
in addition value It's a volatile Variable , Visible in memory , therefore JVM It can guarantee that any thread can always get the latest value of this variable at any time .
Unsafe Object provides a very low-level , Operating memory 、 Thread method ,Unsafe Object cannot be called directly , Only by reflection .jdk8 Call directly
Unsafe.getUnsafe()To obtain the unsafe.
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}
//unsafe.getAndAddInt
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
// Get the old value of the shared variable
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
compareAndSwapInt() Contains three operands :
Memory location (var2)、 The original value of the expected (var5) And the new value ( var5 + var4)
If the value of the memory location matches the expected original value , The processor will automatically update the location value to the new value . Otherwise it will spin until it succeeds .
边栏推荐
- Oracle prompt invalid number
- ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
- Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand
- 河南高考VS天津高考(2008年-2021年)
- 1266_FreeRTOS调度器启动代码实现分析
- 【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
- Leetcode hot topic 100 topic 6-10 solution
- 农房一体脚本的心得记录
- pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
猜你喜欢

Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)

The difference between arrow function and ordinary function

教育专家王中泽老师一招解决学生问题

Implementation of customization function page of online Fox game server room configuration wizard service

河南高考VS天津高考(2008年-2021年)

双周投融报:资本抢滩元宇宙游戏

教育专家王中泽老师多年经验分享:家庭教育不是附庸品

Esp32 learning notes (49) - esp-wifi-mesh interface use

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)

Nodejs database (Part 2)
随机推荐
农房一体脚本的心得记录
Analysis of key points and difficulties of ES6 promise source code
Transformer Tracking
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
Aircraft battle from scratch (III) flight between player aircraft and enemy aircraft
[并发进阶]——线程池总结
saltstack部署zabbix状态文件编写
Object. Specific implementation and difference between create() and new
Stack -- one of two common linear structures of linear structure
.NET C#基础(6):命名空间 - 有名字的作用域
Prototype and prototype chain
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
洛谷P1091合唱队形(最长上升子序列)
Saltstack deployment LNMP
Flat design, blog website (VIII) code source code
【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
Leetcode hot topic 100 topic 21-25 solution
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Summary of string processing skills II