AtomicLong的incrementAndGet()
来源:互联网 发布:mastercam新手编程 编辑:程序博客网 时间:2024/04/30 20:25
调用顺序为:incrementAndGet--->compareAndSet--->compareAndSwapLong
最后这个函数是一个native函数,这个函数中,第一个形参传入的实参为this(调用者),第三和第四个参数中传入实参是incrementAndGet()中读取的两个变量current和next=current+1。因此cas底层的做法是:如果this的值为current,那就把this的值改为current+1。
源代码如下:
/*** Atomically increments by one the current value.
*
* @return the updated value
*/
public final long incrementAndGet() {
for (;;) {
long current = get();
long next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
* @param expect the expected value
* @param update the new value
* @return true if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(long expect, long update) {
return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
}
* 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 compareAndSwapLong(Object o, long offset,
long expected,
long x);
- AtomicLong的incrementAndGet()
- AtomicLong
- AtomicLong
- 线程安全的AtomicLong使用
- AtomicLong和LongAdder的区别
- 线程安全的AtomicLong使用
- 线程安全的AtomicLong使用
- AtomicInteger 中 incrementAndGet与getAndIncrement 两个方法的区别
- AtomicLong.lazySet是如何工作的?
- CAS指令 incrementAndGet方法
- LongAdder的实现以及与AtomicLong的对比
- 比AtomicLong还高效的LongAdder 源码解析
- 比AtomicLong还高效的LongAdder 源码解析
- Java AtomicLong
- AtomicLong介绍
- 分享个java线程安全,自增主键id的类AtomicLong
- java.util.concurrent.atomic 并发包下的原子操作类(AtomicBoolean,AtomicInteger,AtomicLong......))
- JDK 8新特性LongAdder和AtomicLong的性能测试对比
- 克隆Object类
- 求素数
- net use命令详解
- 图解WebSphere 8.5 安装
- 零基础学习安卓【笔记 02 Activity学习】
- AtomicLong的incrementAndGet()
- Handler,MessageQueue,Looper解析
- 不使用临时变量交换数值
- linux中getopts与shift的冲突之处
- linux下如何安装配置JDK
- coredata&加密
- weblogic集群下,程序获取客户端IP、获取用户IP的时,节点需要首先启用WL-Proxy-Client-IP
- Android客户端优化策略
- 安卓笔记