All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.kit.AtomicIntegerKit Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy