日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

JAVA 雪花算法 唯一ID生成工具类

發(fā)布時(shí)間:2024/9/27 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA 雪花算法 唯一ID生成工具类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

package com.gblfy;/*** @Author:JCccc* @Description:* @Date: created in 15:31 2019/6/12*/ public class SnowflakeIdUtils {// ==============================Fields===========================================/** 開始時(shí)間截 (2015-01-01) */private final long twepoch = 1420041600000L;/** 機(jī)器id所占的位數(shù) */private final long workerIdBits = 5L;/** 數(shù)據(jù)標(biāo)識id所占的位數(shù) */private final long datacenterIdBits = 5L;/** 支持的最大機(jī)器id,結(jié)果是31 (這個(gè)移位算法可以很快的計(jì)算出幾位二進(jìn)制數(shù)所能表示的最大十進(jìn)制數(shù)) */private final long maxWorkerId = -1L ^ (-1L << workerIdBits);/** 支持的最大數(shù)據(jù)標(biāo)識id,結(jié)果是31 */private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);/** 序列在id中占的位數(shù) */private final long sequenceBits = 12L;/** 機(jī)器ID向左移12位 */private final long workerIdShift = sequenceBits;/** 數(shù)據(jù)標(biāo)識id向左移17位(12+5) */private final long datacenterIdShift = sequenceBits + workerIdBits;/** 時(shí)間截向左移22位(5+5+12) */private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;/** 生成序列的掩碼,這里為4095 (0b111111111111=0xfff=4095) */private final long sequenceMask = -1L ^ (-1L << sequenceBits);/** 工作機(jī)器ID(0~31) */private long workerId;/** 數(shù)據(jù)中心ID(0~31) */private long datacenterId;/** 毫秒內(nèi)序列(0~4095) */private long sequence = 0L;/** 上次生成ID的時(shí)間截 */private long lastTimestamp = -1L;//==============================Constructors=====================================/*** 構(gòu)造函數(shù)* @param workerId 工作ID (0~31)* @param datacenterId 數(shù)據(jù)中心ID (0~31)*/public SnowflakeIdUtils(long workerId, long datacenterId) {if (workerId > maxWorkerId || workerId < 0) {throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));}if (datacenterId > maxDatacenterId || datacenterId < 0) {throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));}this.workerId = workerId;this.datacenterId = datacenterId;}// ==============================Methods==========================================/*** 獲得下一個(gè)ID (該方法是線程安全的)* @return SnowflakeId*/public synchronized long nextId() {long timestamp = timeGen();//如果當(dāng)前時(shí)間小于上一次ID生成的時(shí)間戳,說明系統(tǒng)時(shí)鐘回退過這個(gè)時(shí)候應(yīng)當(dāng)拋出異常if (timestamp < lastTimestamp) {throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));}//如果是同一時(shí)間生成的,則進(jìn)行毫秒內(nèi)序列if (lastTimestamp == timestamp) {sequence = (sequence + 1) & sequenceMask;//毫秒內(nèi)序列溢出if (sequence == 0) {//阻塞到下一個(gè)毫秒,獲得新的時(shí)間戳timestamp = tilNextMillis(lastTimestamp);}}//時(shí)間戳改變,毫秒內(nèi)序列重置else {sequence = 0L;}//上次生成ID的時(shí)間截lastTimestamp = timestamp;//移位并通過或運(yùn)算拼到一起組成64位的IDreturn ((timestamp - twepoch) << timestampLeftShift) //| (datacenterId << datacenterIdShift) //| (workerId << workerIdShift) //| sequence;}/*** 阻塞到下一個(gè)毫秒,直到獲得新的時(shí)間戳* @param lastTimestamp 上次生成ID的時(shí)間截* @return 當(dāng)前時(shí)間戳*/protected long tilNextMillis(long lastTimestamp) {long timestamp = timeGen();while (timestamp <= lastTimestamp) {timestamp = timeGen();}return timestamp;}/*** 返回以毫秒為單位的當(dāng)前時(shí)間* @return 當(dāng)前時(shí)間(毫秒)*/protected long timeGen() {return System.currentTimeMillis();}//==============================Test=============================================/** 測試 */public static void main(String[] args) {SnowflakeIdUtils idWorker = new SnowflakeIdUtils(3, 1);System.out.println(idWorker.nextId());} }

總結(jié)

以上是生活随笔為你收集整理的JAVA 雪花算法 唯一ID生成工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。