AtomicInteger 中 incrementAndGet与getAndIncrement 两个方法的区别
2016-03-07 15:00
405 查看
通过查看JDK的API知道:
字面解释都一样。
再进行源代码查看:
由此可以看出,两个方法处理的方式都是一样的,区别在于
方法是返回旧值(即加1前的原始值),而
int | incrementAndGet() 以原子方式将当前值加 1。 |
int | getAndIncrement() 以原子方式将当前值加 1。 |
再进行源代码查看:
public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } } public final int incrementAndGet() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return next; } }
由此可以看出,两个方法处理的方式都是一样的,区别在于
getAndIncrement
方法是返回旧值(即加1前的原始值),而
incrementAndGet
返回的是新值(即加1后的值)
相关文章推荐
- linux中vim的使用
- 112.View the Exhibit and examine the structure of the PROMOTIONS table.
- 奔五的人学IOS:swift练手与csdn,最近学习总结
- C#使用LINQ中Enumerable类方法的延迟与立即执行的控制
- 端口号、进程号之间关系
- android:windowSoftInputMode属性解析
- NSURLSession的基本使用
- 从 Qt 的 delete 说开来
- EditText使用技巧
- javascript操作cookie
- 通过本地加载ga.js文件提高Google Anlytics性能
- 辛星解读mysql的用户管理
- 通过阅读教材,所得的不懂的问题
- postgresql常用命令
- 体绘制之光线投射算法(附源码)
- 机器人的运动范围
- 项目开发经验总结
- 这篇文章给大家介绍Android开发四大组件之实现电话拦截和电话录音,涉及到android四大基本组件在程序中的应用,对android四大基本组件感兴趣的朋友可以参考下本篇文章
- 如何解决GIF流量爆炸问题?
- MySQL5.6 PERFORMANCE_SCHEMA 说明