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

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2021-10-22
  • 2022-02-09
  • 2021-04-27
猜你喜欢
  • 2021-05-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-10-15
  • 2021-08-21
  • 2021-05-27
相关资源
相似解决方案