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


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后的值。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM