com.feingto.cloud.kit.AtomicIntegerKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* AtomicInteger 计算工具类
*
* @author longfei
*/
public class AtomicIntegerKit {
private static final AtomicInteger latch = new AtomicInteger(0);
private static int increment(int index, boolean isNext) {
for (; ; ) {
int current = latch.get();
int next = (current + 1) % index;
if (latch.compareAndSet(current, next)) {
return isNext ? next : current;
}
}
}
public static int getAndIncrement(int index) {
return increment(index, false);
}
public static int incrementAndGet(int index) {
return increment(index, true);
}
}