snowflake java_Java 实现的SnowFlake生成UUID (Java代码实战-007)

importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;/*** Created by xfyou 2018/6/8 14:01.*/

public classSnowFlakeIdGenerator {//The initial time(2017-01-01)

private static final long INITIAL_TIME_STAMP = 1483200000000L;//Machine ID bits

private static final long WORK_ID_BITS = 5L;//DataCenter ID bits

private static final long DATACENTER_ID_BITS = 5L;//The maximum machine ID supported is 31, which can quickly calculate the maximum decimal number that a few binary numbers can represent.

private static final long MAX_WORKER_ID = ~(-1L <

private static final long MAX_DATACENTER_ID = ~(-1L <

private static final long SEQUENCE_BITS = 12L;//The machine ID offset,12

private static final long WORKERID_OFFSET =SEQUENCE_BITS;//The datacent ID offset,12+5

private static final long DATACENTERID_OFFSET = WORK_ID_BITS +SEQUENCE_BITS;//The timestape offset, 5+5+12

private static final long TIMESTAMP_OFFSET = DATACENTER_ID_BITS + WORK_ID_BITS +SEQUENCE_BITS;//Sequence mask,4095

private static final long SEQUENCE_MASK = ~(-1L <

private longworkerId;//Datacenter ID,0~31

private longdatacenterId;//Sequence,0~4095

private long sequence = 0L;//Last timestamp

private long lastTimestamp = -1L;public SnowFlakeIdGenerator(long workerId, longdatacenterId) {if (workerId > MAX_WORKER_ID || workerId < 0)throw new IllegalArgumentException(String.format("WorkerID can't be greater than %d or less than 0", MAX_WORKER_ID));if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0)throw new IllegalArgumentException(String.format("DataCenterID can't be greater than %d or less than 0", MAX_WORKER_ID));this.workerId =workerId;this.datacenterId =datacenterId;

}public synchronized longnextId() {long timeStamp =System.currentTimeMillis();if (timeStamp

sequence= (sequence + 1) &SEQUENCE_MASK;if (0 ==sequence)

timeStamp=tillNextMillis(lastTimestamp);

}else{

sequence= 0L;

}

lastTimestamp=timeStamp;return (timeStamp - INITIAL_TIME_STAMP) << TIMESTAMP_OFFSET | (datacenterId << DATACENTERID_OFFSET) | (workerId << WORKERID_OFFSET) |sequence;

}private long tillNextMillis(longlastTimestamp) {long timestamp =System.currentTimeMillis();while (timestamp <=lastTimestamp)

timestamp=System.currentTimeMillis();returntimestamp;

}public static voidmain(String[] args) {

SnowFlakeIdGenerator generator= new SnowFlakeIdGenerator(1, 1);

ExecutorService executorService=Executors.newCachedThreadPool();for (int i = 0; i < 5; i++) {

executorService.execute(newRunnable() {

@Overridepublic voidrun() {long id =generator.nextId();

System.out.println(id);

}

});

}

executorService.shutdown();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值