Java 自动生成16位Ling类型id 发表于 2020-07-03 | 次阅读 字数统计: 463 字 | 阅读时长 ≈ 3 min 下载地址 自动生成16位Ling类型id使用1ID16NumberGenerator.get16NumberID() 源码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125package com.wangjikai.util;import org.apache.commons.lang3.time.DateFormatUtils;/*import org.slf4j.Logger;import org.slf4j.LoggerFactory;*/import java.util.*;public class ID16NumberGenerator { private static ID16NumberGenerator instance = new ID16NumberGenerator(); //private Logger logger = LoggerFactory.getLogger(this.getClass()); private long time = 0L; private int seq = 101; private ID16NumberGenerator() { } public static Long get16NumberID() { ID16NumberGenerator var0 = instance; synchronized(instance) { return instance.generate16Id(); } } public static int getNRdmNumber(int n) { if (n > 10) { n = 10; } else if (n < 1) { n = 1; } int min = (int)Math.pow(10.0D, (double)(n - 1)); int max = (int)Math.pow(10.0D, (double)n) - 1; return getVectorNum(max, min); } public static Set<Long> get16NumberID(int count) { int size = count > 0 ? count : 1; TreeSet ids = new TreeSet(); while(ids.size() < size) { ids.add(get16NumberID()); } return ids; } private static synchronized int getVectorNum(int max, int min) { Random random = new Random(); int s1 = random.nextInt(max) % (max - min + 1) + min; return s1; } public static String getPrchaseNum() { String timeString = DateFormatUtils.format(new Date(), "yyMMdd"); String result = "CG" + timeString + getVectorNum(999999, 100000); return result; } private Long generate16Id() { long currentTime = System.currentTimeMillis(); if (currentTime == this.time) { ++this.seq; } else { this.seq = 101; this.time = currentTime; } if (this.seq > 999) { try { Thread.sleep(1L); } catch (InterruptedException var4) { //this.logger.warn(var4.getMessage(), var4); } return this.generate16Id(); } else { return this.time * 1000L + (long)this.seq; } } /** * 获取uuid * * @return uuid * */ public static String getUuid(){ // uuid String uuid = UUID.randomUUID().toString(); return uuid; } public static void main(String[] arg) throws InterruptedException { for(int i = 0; i < 10; ++i) { System.out.println(ID16NumberGenerator.get16NumberID()); } for(int i = 0; i < 10; ++i) { // System.out.println(getNRdmNumber(2)); Thread.sleep(1); System.out.println(ID16NumberGenerator.get16NumberID()); } System.out.println(ID16NumberGenerator.get16NumberID()); Set<Integer> ints = new TreeSet(); for(int i = 0; i < 10; ++i) { System.out.println(getNRdmNumber(2)); } Iterator i$ = ints.iterator(); while(i$.hasNext()) { Integer num = (Integer)i$.next(); System.out.println(num); } System.out.println(ints.size()); }} 欢迎加我的微信,共同交流技术