IdGenerator id主键生成器

import java.util.concurrent.locks.ReentrantLock;

public class IdGenerator {
    private static final long twepoch = 1404741526058L;
    private static final long idBits = 5L;
    private static final long groupIdBits = 5L;
    private static final long sequenceBits = 12L;
    private static final long maxId = 31L;
    private static final long maxGroupId = 31L;
    private static final long sequenceMask = 4095L;
    private static final long idShift = 12L;
    private static final long groupIdShift = 17L;
    private static final long timestampLeftShift = 22L;
    private static long lastTimestamp = -1L;
    private ReentrantLock lock = new ReentrantLock();
    private long sequence = 0L;
    private final long databaseId;
    private final long dbGroupId;
    private static ThreadLocal<IdGenerator> defaultIdGenerator = new ThreadLocal();

    public static IdGenerator getDefault() {
        if (defaultIdGenerator.get() == null) {
            defaultIdGenerator.set(new IdGenerator(Thread.currentThread().getId() % 31L, Thread.currentThread().getId() % 31L));
        }

        return (IdGenerator)defaultIdGenerator.get();
    }

    public IdGenerator(long groupId, long id) {
        if (id <= 31L && id >= 0L) {
            if (groupId <= 31L && groupId >= 0L) {
                this.databaseId = id;
                this.dbGroupId = groupId;
            } else {
                throw new IllegalArgumentException("集群Id不能大于%d或小于0");
            }
        } else {
            throw new IllegalArgumentException("Id不能大于%d或小于0");
        }
    }

    public long nextId() {
        this.lock.lock();

        long var5;
        try {
            long timestamp = this.timeGen();
            if (timestamp < lastTimestamp) {
                throw new RuntimeException("系统时间倒退 " + (lastTimestamp - timestamp) + "毫秒,不允许生成id。");
            }

            if (lastTimestamp == timestamp) {
                this.sequence = this.sequence + 1L & 4095L;
                if (this.sequence == 0L) {
                    timestamp = this.toNextMillis(lastTimestamp);
                }
            } else {
                this.sequence = 0L;
            }

            lastTimestamp = timestamp;
            long nextId = timestamp - 1404741526058L << 22 | this.dbGroupId << 17 | this.databaseId << 12 | this.sequence;
            var5 = nextId;
        } finally {
            this.lock.unlock();
        }

        return var5;
    }

    private long toNextMillis(long lastTimestamp) {
        long timestamp;
        for(timestamp = this.timeGen(); timestamp <= lastTimestamp; timestamp = this.timeGen()) {
            ;
        }

        return timestamp;
    }

    private long timeGen() {
        return System.currentTimeMillis();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值