当前位置:网站首页>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 .
边栏推荐
- Aircraft battle from scratch (III) flight between player aircraft and enemy aircraft
- latex 各种箭头/带文字标号的箭头/可变长箭头
- The difference between TCP and UDP
- Detailed explanation of mutationobserver
- Implementation of customization function page of online Fox game server room configuration wizard service
- es5和es6的学习小记
- Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
- Duality-Gated Mutual Condition Network for RGBT Tracking
- Promise details
- 3.1 测试函数的命名规则
猜你喜欢

你知道IT人才外派服务报价是怎样的么?建议程序员也了解下

Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。

1266_ Implementation analysis of FreeRTOS scheduler startup code

网狐游戏服务器房间配置约战定制功能实现

saltstack部署lnmp

教育专家王中泽老师:家庭教育重在自己成长

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

Transformer Tracking

Stack -- one of two common linear structures of linear structure

matplotlib的cmap
随机推荐
Leetcode-141. Linked List Cycle
Luogu p1091 chorus formation (longest ascending subsequence)
【LeetCode】-- 17.电话号码的字母组合
22年五月毕设
Pytest自动化测试-简易入门教程(01)
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
Practice: how to reasonably design functions to solve practical problems in software development (II) -- improving reusability
Listen to the left width of the browser to calculate the distance
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
The meaning and research significance of mathematical methodology
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
The realization of online Fox game server room configuration battle engagement customization function
Pytest automated test - easy tutorial (01)
Janus feature草稿
Analysis of key points and difficulties of ES6 promise source code
Required reading 1: the larger the pattern, the better they know how to speak
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
Summary and review
How to make time planning
模块化笔记