2019独角兽企业重金招聘Python工程师标准>>>
直接上代码(这段代码我是抄的,虽然看不懂,但是我知道能用,哈哈)
package com.example.learning.雪花算法;/*** 描述:雪花算法* User: 曾远征* Date: 2018-09-17* Time: 15:51*/
public class SnowflakesTools {//开始时间截 (2015-01-01)private final long twepoch &#61; 1489111610226L;//机器ID所占位置private final long workerIdBits &#61; 5L;//数据标识所占位数private final long datacenterIdBits &#61; 5L;//支持的最大机器id&#xff0c;结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)private final long maxWorkerId &#61; -1L ^ (-1L < maxWorkerId || workerId <0) {throw new IllegalArgumentException(String.format("worker Id can&#39;t be greater than %d or less than 0",maxWorkerId));}if (datacenterId > maxDatacenterId || datacenterId <0) {throw new IllegalArgumentException(String.format("datacenter Id can&#39;t be greater than %d or less than 0",maxDatacenterId));}this.workerId &#61; workerId;this.datacenterId &#61; datacenterId;}/*** 获得下一个ID (该方法是线程安全的)* &#64;return*/public synchronized long nextId() {long timestamp &#61; timeGen();//如果当前时间小于上一次ID生成的时间戳&#xff0c;说明系统时钟回退过这个时候应当抛出异常if (timestamp