AtomicInteger越界问题
AtomicInteger的incrementAndGet方法不断的增加,并没有注意到如果AtomicInteger增加到了2147483647(Integer.MAX_VALUE)再加一,AtomicInteger的值会变成负数-2147483648(Integer.MIN_VALUE)。如果不对其作出处理,当资源数目不断累积超过最大值变成负数的时候,最后产生的Id中会带有一个“-”
public int getAndIncrement() { int current; int next; do { current = counter.get(); next = current >= Integer.MAX_VALUE ? 0 : current + 1; } while (!counter.compareAndSet(current, next)); return current; }
立志如山 静心求实
浙公网安备 33010602011771号