首页 > 其他 > 详细

AtomicInteger 中 incrementAndGet与getAndIncrement 两个方法的区别

时间:2019-06-23 15:50:43      阅读:352      评论:0      收藏:0      [点我收藏+]

https://blog.csdn.net/chenkaibsw/article/details/81031950

 

看源码:

getAndIncrement
1 public final int getAndIncrement() {
2 for (;;) {
3 int current = get();
4 int next = current + 1;
5 if (compareAndSet(current, next))
6 return current;
7 }
8 }

incrementAndGet
1 public final int incrementAndGet() {
2 for (;;) {
3 int current = get();
4 int next = current + 1;
5 if (compareAndSet(current, next))
6 return next;
7 }
8 }

通过代码可以看出:

getAndIncrement返回的是当前值;
incrementAndGet返回的是加1后的值。

AtomicInteger 中 incrementAndGet与getAndIncrement 两个方法的区别

原文:https://www.cnblogs.com/gaoquanquan/p/11072850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!