当前位置:网站首页>JUC Unsafe
JUC Unsafe
2022-06-13 09:05:00 【Q z1997】
summary
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;
}
}
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);```
```java
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;
}
}
边栏推荐
- Three indexes reflecting system reliability in performance test: MTTF, MTTR and MTBF
- Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
- Redirect vulnerability analysis of network security vulnerability analysis
- 基于微信小程序的图书馆管理系统.rar(论文+源码)
- ""? "& in URL Role of "" sign
- JS obtain geographic location information according to longitude and latitude and mark it on the map
- Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
- 【安全】零基礎如何從0到1逆襲成為安全工程師
- Bash: kill: (74001) - operation not allowed
- Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
猜你喜欢

教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5

20211104 why are the traces of similar matrices the same

JUC 原子累加器 源码之 LongAdder

Uni app subcontracting loading and optimization

Neo4j - CQL use

【网络安全】SQL注入新思维之webshell提权

Mttr/mttf/mtbf diagram

网络安全漏洞分析之重定向漏洞分析

【网络安全渗透】如果你还不懂CSRF?这一篇让你彻底掌握

torch. How to calculate addmm (m, mat1, mat2)
随机推荐
output. Interpretation of topk() function
20211006 线性变换
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
20211018 some special matrices
消息中间件
Pytorch same structure different parameter name model loading weight
Redis fuzzy query batch deletion
20211006 integral, differential and projection belong to linear transformation
JUC原子整数
20211108 微分跟踪器
Implement authentication code login and remember password (cookie)
13.inline,const,mutable,this,static
Knowledge points related to system architecture 3
redis 模糊查询 批量删除
Web page H5 wechat sharing
pytorch统计模型的参数个数
Brief description of port, domain communication port and domain service
Cesium view switching, locating, reading files, building data sources, entity control, model control, etc
基于微信小程序的图书馆管理系统.rar(论文+源码)
Tensorflow1.14 corresponds to numpy version