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

com.feingto.iot.common.util.id.IncrementIdGenerator Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.common.util.id;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自增ID生成实现
 *
 * @author longfei
 */
public class IncrementIdGenerator implements IdGenerator {
    private static AtomicInteger index = new AtomicInteger(1);

    private static int messageId() {
        for (; ; ) {
            int current = index.get();
            int next = (current >= Integer.MAX_VALUE ? 0 : current + 1);
            if (index.compareAndSet(current, next)) {
                return current;
            }
        }
    }

    @Override
    public Integer nextId() {
        return messageId();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy