当前位置:网站首页>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;
}
}
边栏推荐
- Subspace of 20211004 matrix
- Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
- Gbase 8A disk problems and Solutions
- Agile development practice summary-4
- 14. class initialization, default constructor, =default
- System analysis - detailed description
- 20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
- Download address of QT source code of each version
- 20211028 Stabilizability
- Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
猜你喜欢

Completely uninstall PostgreSQL under Linux

Cesium achieves sunny, rainy, foggy, snowy and other effects

Redirect vulnerability analysis of network security vulnerability analysis

Animation through svg

Uni app essay

消息中间件

Simulink如何添加模块到Library Browser

Diversified tables through TL table row consolidation

How to become a white hat hacker? I suggest you start from these stages

Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
随机推荐
Software Architecture Overview knowledge
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
Drill down to protobuf - Introduction
an error occurred while trying to rename a file in the destination directory code 5
The Jenkins console does not output custom shell execution logs
如何成为白帽子黑客?我建议你从这几个阶段开始学习
Object in ES6 Use of entries()
20211108 det(AB)=det(A)det(B)
消息中间件
An error CV2 is reported when the picture is converted to grayscale cvtColor(img, cv2.COLOR_BGR2GRAY)
C language time difference calculation
Cesium displays a pop-up box at the specified position and moves with the map
final 原理
20211020 段院士全驱系统
Gbase 8A v95 vs v86 compression strategy analogy
turf. JS usage
20211006 线性变换
20211006 linear transformation
Number of parameters of pytorch statistical model
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积