雪花算法生成id的方法:首先新建一个id生成的类;然后新建一个调用生成id的单例工具;最后使用函数【guuidutil.getuuid()】直接调用即可。
【相关学习推荐:java基础】
雪花算法生成id的方法:
1、新建一个id生成的类 snowflake
/ * @auther: lyl * @date: 2019/11/21 17:49 * @description: */public class snowflake { / * 起始的时间戳 */ private final static long start_stmp = 1480166465631l; / * 每一部分占用的位数 */ private final static long sequence_bit = 12; //序列号占用的位数 private final static long machine_bit = 5; //机器标识占用的位数 private final static long datacenter_bit = 5;//数据中心占用的位数 / * 每一部分的最大值 */ private final static long max_datacenter_num = -1l ^ (-1l << datacenter_bit); private final static long max_machine_num = -1l ^ (-1l << machine_bit); private final static long max_sequence = -1l ^ (-1l << sequence_bit); / * 每一部分向左的位移 */ private final static long machine_left = sequence_bit; private final static long datacenter_left = sequence_bit machine_bit; private final static long timestmp_left = datacenter_left datacenter_bit; private long datacenterid; //数据中心 private long machineid; //机器标识 private long sequence = 0l; //序列号 private long laststmp = -1l;//上一次时间戳 public snowflake(long datacenterid, long machineid) { if (datacenterid > max_datacenter_num || datacenterid < 0) { throw new illegalargumentexception("datacenterid can't be greater than max_datacenter_num or less than 0"); } if (machineid > max_machine_num || machineid < 0) { throw new illegalargumentexception("machineid can't be greater than max_machine_num or less than 0"); } this.datacenterid = datacenterid; this.machineid = machineid; } / * 产生下一个id * * @return */ public synchronized long nextid() { long currstmp = getnewstmp(); if (currstmp < laststmp) { throw new runtimeexception("clock moved backwards. refusing to generate id"); } if (currstmp == laststmp) { //相同毫秒内,序列号自增 sequence = (sequence 1) & max_sequence; //同一毫秒的序列数已经达到最大 if (sequence == 0l) { currstmp = getnextmill(); } } else { //不同毫秒内,序列号置为0 sequence = 0l; } laststmp = currstmp; return (currstmp - start_stmp) << timestmp_left //时间戳部分 | datacenterid << datacenter_left //数据中心部分 | machineid << machine_left //机器标识部分 | sequence; //序列号部分 } private long getnextmill() { long mill = getnewstmp(); while (mill <= laststmp) { mill = getnewstmp(); } return mill; } private long getnewstmp() { return system.currenttimemillis(); } public static void main(string[] args) { snowflake snowflake = new snowflake(2, 3); for (int i = 0; i < (1 << 12); i ) { system.out.println(snowflake.nextid()); } }}2、为防止多线程生成重复的id,这边新建了一个调用生成id的单例工具
其中machineid和datacenterid可以放在配置文件中
import java.util.concurrent.countdownlatch;/ * @auther: lyl * @date: 2019/11/21 18:15 * @description: */public class guuidutil { private static long machineid = 0; private static long datacenterid = 0; / * 单例模式创建学法算法对象 * */ private enum snowflakesingleton{ singleton; private snowflake snowflake; snowflakesingleton(){ snowflake = new snowflake(datacenterid,machineid); } public snowflake getinstance(){ return snowflake; } } public static long getuuid(){ return snowflakesingleton.singleton.getinstance().nextid(); } public static void main(string[] args) { countdownlatch latch = new countdownlatch(10000); long start = system.currenttimemillis(); for (int i = 0; i < 10000; i ) { new runnable() {
云服务器收费情况华为云云服务器超低优惠哪部的云服务器四核8g的最便宜Deno中如何使用 Node 模块?腾讯云轻量服务器能买几个域名实名制太慢了-域名及账户问题腾讯云服务器网站默认页面兴安盟云服务器购买