当前位置:网站首页>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;
}
}
边栏推荐
- 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
- 20220524 how to install coppeliasim to disk D
- 20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
- Implement authentication code login and remember password (cookie)
- Neo4j環境搭建
- Talking about acid of database
- 网上开户安全吗?新手可以开账户吗?
- Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
- Gbase 8A disk problems and Solutions
- How excel adds hyperlinks to some text in a cell
猜你喜欢

Knowledge points related to system architecture 1

How many TCP connections can a machine create at most?

Loss outputs Nan for the Nan model
![[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation](/img/4d/c33a3fcc3b45c2369e1f1f74a8d51b.png)
[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation

Neo4j - CQL use

QT multithreaded TCP server

QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere

Completely uninstall PostgreSQL under Linux

20211020 academician all drive system

transforms. ColorJitter(0.3, 0, 0, 0)
随机推荐
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
20211005 Hermite矩阵及几个性质
Uni app essay
QML compilation specification
torch. How to calculate addmm (m, mat1, mat2)
JUC Unsafe
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
MySQL startup error: innodb: operating system error number 13 in a file operation
""? "& in URL Role of "" sign
20211018 一些特殊矩阵
pytorch统计模型的参数个数
How to become a white hat hacker? I suggest you start from these stages
消息中间件
Cesium common events, including click events, mouse events, and camera movement events
Judgment of single exclamation point and double exclamation point in JS
What are the bank financial products? How long is the liquidation period?
Brief description of port, domain communication port and domain service
Void* pointer
20220524 how to install coppeliasim to disk D
CAS无锁