java 工具类,生成唯一的id值

2022-08-14 11:01:57 浏览数 (1)

目录

  • 1 代码

1 代码

代码语言:javascript复制
public class IdGenerator {

    public static final long WORKER_ID = ipKeyGenerator();
    private static long sequence;
    private static long lastTimestamp = -1L;

    public static synchronized String generateId() {
        long currentMillis = currentMillis();
        if (lastTimestamp == currentMillis) {
            sequence = sequence   1L & 4095L;
            if (sequence == 0L) {
                currentMillis = waitUntilNextTime(currentMillis);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = currentMillis;
        long nextId = currentMillis - 1295884800000L << 22 | WORKER_ID << 12 | sequence;
        return String.valueOf(nextId);
    }

    private static long waitUntilNextTime(long lastTime) {
        long timestamp;
        for (timestamp = currentMillis(); timestamp <= lastTime; timestamp = currentMillis()) {
        }
        return timestamp;
    }

    private static long ipKeyGenerator() {
        InetAddress address;
        try {
            address = InetAddress.getLocalHost();
        } catch (final UnknownHostException e) {
            throw new IllegalStateException("cannot get localhost address, please check your network!");
        }
        byte[] ipAddressByteArray = address.getAddress();
        return ((long) (((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE)   (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF)));
    }

    private static long currentMillis() {
        return System.currentTimeMillis();
    }

    public static void main(String[] args) {
        System.out.println(generateId());
    }
}

0 人点赞