Unsafe.compareAndSwapInt()方法具有什么功能呢?
下文笔者讲述Unsafe.compareAndSwapInt()方法的功能简介说明,如下所示
Unsafe.compareAndSwapInt()方法源码简介
/**
* Atomically update Java variable to <tt>x</tt> if it is currently
* holding <tt>expected</tt>.
* @return <tt>true</tt> if successful
*/
public final native boolean compareAndSwapInt(Object o, long offset,
int expected,
int x);
compareAndSwapInt方法简介:
此方法是Java的native方法,是JVM实现的方法
此方法的功能:读取传入对象o在内存中偏移量为offset位置的值与期望值expected作比较
当相等就把x值赋值给offset位置的值,返回true
当不相等,就取消赋值,方法返回false
此方法是实现CAS,最重要的方法,用于保证"无锁并发安全性"
是轻量级Lock的实现底层方法
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


