当前位置:网站首页>Multithreading tutorial (XXVIII) unsafe class
Multithreading tutorial (XXVIII) unsafe class
2022-06-11 05:29:00 【Have you become a great God today】
Multithreading tutorial ( Twenty-eight )unsafe class
Unsafe Object provides a very low-level , Operating memory 、 Thread method ,Unsafe Object cannot be called directly , Only by reflection
public class UnsafeAccessor {
static Unsafe unsafe;
static {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
unsafe = (Unsafe) theUnsafe.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new Error(e);
}
}
static Unsafe getUnsafe() {
return unsafe;
}
}
1.Unsafe CAS operation
@Data
class Student {
volatile int id;
volatile String name; }
Unsafe unsafe = UnsafeAccessor.getUnsafe();
Field id = Student.class.getDeclaredField("id");
Field name = Student.class.getDeclaredField("name");
// Get the offset of the member variable
long idOffset = UnsafeAccessor.unsafe.objectFieldOffset(id);
long nameOffset = UnsafeAccessor.unsafe.objectFieldOffset(name);
Student student = new Student();
// Use cas Method to replace the value of a member variable
UnsafeAccessor.unsafe.compareAndSwapInt(student, idOffset, 0, 20); // return true
UnsafeAccessor.unsafe.compareAndSwapObject(student, nameOffset, null, " Zhang San "); // return true
System.out.println(student);
Output
Student(id=20, name= Zhang San )
Use custom AtomicData Thread safe atomic integers before implementation Account Realization
class AtomicData {
private volatile int data;
static final Unsafe unsafe;
static final long DATA_OFFSET;
static {
unsafe = UnsafeAccessor.getUnsafe();
try {
// data Attribute in DataContainer Offset in object , be used for Unsafe Access the property directly
DATA_OFFSET = unsafe.objectFieldOffset(AtomicData.class.getDeclaredField("data"));
} catch (NoSuchFieldException e) {
throw new Error(e);
}
}
public AtomicData(int data) {
this.data = data;
}
public void decrease(int amount) {
int oldValue;
while(true) {
// Get the old value of the shared variable , You can add breakpoints to this line , modify data Debug to deepen understanding
oldValue = data;
// cas Try to modify data by The old value + amount, If the old value is changed by another thread , return false
if (unsafe.compareAndSwapInt(this, DATA_OFFSET, oldValue, oldValue - amount)) {
return;
}
}
}
public int getData() {
return data;
}
}
Account Realization
Account.demo(new Account() {
AtomicData atomicData = new AtomicData(10000);
@Override
public Integer getBalance() {
return atomicData.getData();
}
@Override
public void withdraw(Integer amount) {
atomicData.decrease(amount);
}
});
边栏推荐
- 使用acme.sh自动申请免费SSL证书
- Overview of self attention acceleration methods: Issa, CCNET, cgnl, linformer
- 35.搜索插入位置
- Use acme SH automatically apply for a free SSL certificate
- Yolov5 training personal data set summary
- [entry level basics] node basic knowledge summary
- Es IK installation error
- Recursively process data accumulation
- 微信小程序,购买商品属性自动换行,固定div个数,超出部分自动换行
- KD-Tree and LSH
猜你喜欢

22. Generate parentheses

微信小程序text内置组件换行符不换行的原因-wxs处理换行符,正则加段首空格

Deep search + backtracking

Restoration of binary tree -- number restoration

(15) Infrared communication

English digital converter

Cascade EF gan: local focus progressive facial expression editing

Recommend a free intranet penetration open source software that can be used in the local wechat official account under test

如何让灯具智能化,单火、零火智能开关怎么选!

Zed2 camera manual
随机推荐
Combing route - Compaction Technology
Cross modal retrieval | visual representation learning
Concurrent search set
Flask develops and implements the like comment module of the online question and answer system
深度学习分布式训练
About custom comparison methods of classes and custom methods of sort functions
22、生成括号
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
Cascade EF gan: local focus progressive facial expression editing
Carrier coordinate system inertial coordinate system world coordinate system
IOU series (IOU, giou, Diou, CIO)
wxParse解析iframe播放视频
Chapter I: Net architecture (1)
Convert result set of SQL to set
Share | defend against physically realizable image classification attacks
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
GAMES101作业7-Path Tracing实现过程&代码详细解读
2021 iccv paper sharing - occlusion boundary detection
Some details about memory
Customize the layout of view Foundation