AtomicInteger的越界问题

amber 4年前 2,597次点击 来自 技术

背景
AtomicInteger的incrementAndGet方法不断的增加,并没有注意到如果AtomicInteger增加到了2147483647(Integer.MAX_VALUE)再加一,AtomicInteger的值会变成负数-2147483648(Integer.MIN_VALUE)。如果不对其作出处理,当资源数目不断累积超过最大值变成负数的时候,这将带来灾难性的后果。

解决方案

在AtomicInteger变量达到最大值的时候,转为零重新开始计数。并保证AtomicInteger在多线程环境下的原子性。代码如下,compareAndSet方法讲解见下面。

private final AtomicInteger i;
public final int incrementAndGet() {
    int current;
    int next;
    do {
        current = this.i.get();
        next = current >= 2147483647?0:current + 1;
    } while(!this.i.compareAndSet(current, next));

    return next;
}

public final int decrementAndGet() {
    int current;
    int next;
    do {
        current = this.i.get();
        next = current <= 0?2147483647:current - 1;
    } while(!this.i.compareAndSet(current, next));

    return next;
}

compareAndSet

AtomicInteger类的compareAndSet方法是会去调用Unsafe类的compareAndSwapInt方法,而compareAndSwapInt方法是一个本地方法

public final boolean compareAndSet(int expect, int update) {
   return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
目前暂无回复

致力于搭建一个分享发现好玩东西的社区京ICP备15052899号-3

本站总访问量 | 本文总阅读量 | 本站总访客数