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