Advertisement

多线程- Atomic中的incrementAndGet与getAndIncrement方法区别

阅读量:

int incrementAndGet()

将当前值以原子操作的方式增加 1。

int getAndIncrement()

将当前数值以原子操作的方式进行递增处理。

再进行源代码查看:

复制代码
    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;
    }
    }
    
    
      
      
      
      
      
      
      
      
      
      

全部评论 (0)

还没有任何评论哟~