当前位置:网站首页>JUC Unsafe
JUC Unsafe
2022-06-13 09:00:00 【Q z1997】
概述
Unsafe 对象提供了非常底层的,操作内存、线程的方法,Unsafe 对象不能直接调用,只能通过反射获得
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 操作
@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");
// 获得成员变量的偏移量
long idOffset = UnsafeAccessor.unsafe.objectFieldOffset(id);
long nameOffset = UnsafeAccessor.unsafe.objectFieldOffset(name);
Student student = new Student();
// 使用 cas 方法替换成员变量的值
UnsafeAccessor.unsafe.compareAndSwapInt(student, idOffset, 0, 20); // 返回 true
UnsafeAccessor.unsafe.compareAndSwapObject(student, nameOffset, null, "张三"); // 返回 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 属性在 DataContainer 对象中的偏移量,用于 Unsafe 直接访问该属性
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) {
// 获取共享变量旧值,可以在这一行加入断点,修改 data 调试来加深理解
oldValue = data;
// cas 尝试修改 data 为 旧值 + amount,如果期间旧值被别的线程改了,返回 false
if (unsafe.compareAndSwapInt(this, DATA_OFFSET, oldValue, oldValue - amount)) {
return;
}
}
}
public int getData() {
return data;
}
}
边栏推荐
- 20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
- Neo4j - CQL使用
- 14. class initialization, default constructor, =default
- Redirect vulnerability analysis of network security vulnerability analysis
- What exactly is Huawei cloud desktop saying when it says "smooth"?
- Talking about acid of database
- 20211020 段院士全驱系统
- ES6 module import / export summary
- Neo4j environment construction
- 网上开户安全吗?新手可以开账户吗?
猜你喜欢

Neo4j - CQL使用

基于微信小程序的图书馆管理系统.rar(论文+源码)

教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5

Knowledge points related to system architecture 1

Detailed explanation of C language callback function

JUC原子引用与ABA问题

The Jenkins console does not output custom shell execution logs

Mttr/mttf/mtbf diagram

Mapbox usage, including drawing, loading, modifying, deleting points and faces, displaying pop ups, etc

教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5
随机推荐
You don't know the usage of stringstream
File upload JS
Two good kids
Brief description of port, domain communication port and domain service
Gbase 8A disk problems and Solutions
类的加载概述
「解读」华为云桌面说“流畅”的时候,究竟在说什么?
GBase 8a V95与V86压缩策略类比
Collection of various books
Download address of QT source code of each version
A very detailed blog about the implementation of bilinear interpolation by opencv
图数据库Neo4j介绍
Paging query template of Oracle
5. Attribute selector
你不知道的stringstream的用法
20211018 some special matrices
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
Can I open an account for the reverse repurchase of treasury bonds? Can I directly open the security of securities companies on the app for the reverse repurchase of treasury bonds? How can I open an
Summary of the first retrospective meeting
""? "& in URL Role of "" sign