当前位置:网站首页>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 .
边栏推荐
- Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch
- The meaning and research significance of mathematical methodology
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
- 1266_ Implementation analysis of FreeRTOS scheduler startup code
- Saltstack deployment LNMP
- ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
- Notice on organizing the application for the first edition of Ningbo key software in 2022
- Aircraft battle from scratch (III) flight between player aircraft and enemy aircraft
- 12. integer to Roman numeral
- 【迅为干货】龙芯2k1000开发板opencv 测试
猜你喜欢

. Net C Foundation (6): namespace - scope with name

开源漫画服务器Mango

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

Leetcode-104. Maximum Depth of Binary Tree

Menu double linkage effect in uniapp
![[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen](/img/93/d1014b07c924195e062dc83d69b14a.png)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen

网狐游戏服务器房间配置向导服务定制功能页实现

Records how cookies are carried in cross domain requests

Leetcode hot topic 100 topic 6-10 solution

品牌定位个性六种形态及结论的重大意义
随机推荐
**Count the characters with the largest number of words**
Common troubleshooting tools and analysis artifacts are worth collecting
Graph Attention Tracking
Esp32 learning notes (49) - esp-wifi-mesh interface use
Appclips & tips (continuous update)
Leetcode-141. Linked List Cycle
Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
Menu double linkage effect in uniapp
Detailed explanation of mutationobserver
生物序列智能分析平台blog(1)
Interview question 17.08 Circus tower
sql查询问题,只显示列名 不显示数据
Notice on organizing the application for the first edition of Ningbo key software in 2022
client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
Required reading 1: the larger the pattern, the better they know how to speak
Heartless sword Chinese English bilingual poem 001 Love
A promise with bare hands
The realization of online Fox game server room configuration battle engagement customization function
[matlab WSN communication] a_ Star improved leach multi hop transmission protocol [including source code phase 487]