单机环境下唯一流水号生成方式---独自生成流水号mac+时间+...


===========================流水号生成方式:采用MAC地址+System.currentTimeMillis()+System.nanoTime();

/**
     * 流水号 66位 
     *     采用MAC地址+System.currentTimeMillis()+System.nanoTime();
     * @return 000000000000000000000000092378948631305 1642495488283 27272453110808  39+13+14=66
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:10000
     *        冲突数:0
     */
    public static String serialMode1() {
        NetworkInterface netInterface = null;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac = new BigInteger(netInterface.getHardwareAddress()).longValue();
        }catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String str = String.format("%039d%013d%013d",mac,System.currentTimeMillis(),System.nanoTime());
        return str;
    }

===========================采用MAC地址+yyyyMMddHHmmssSSS+System.nanoTime();

/**
     * 流水号 70位 
     *     采用MAC地址+yyyyMMddHHmmssSSS+System.nanoTime();
     * @return 000000000000000000000000092378948631305 20220118164448447 27272619297514  39+17+14=70
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:10000
     *        冲突数:0
     */
    public static String serialMode2() {
        NetworkInterface netInterface = null;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac = new BigInteger(netInterface.getHardwareAddress()).longValue();
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String str = String.format("%039d%s%013d",mac,DateFormatUtil.formatCurDate(DateFormatUtil.DATE_PATTERN_yyyyMMddHHmmssSSS),System.nanoTime());
        return str;
    }

===========================采用MAC地址+System.currentTimeMillis()+Math.Random()*1000;

    /**
     * 流水号 56位 
     *     采用MAC地址+System.currentTimeMillis()+Math.Random()*1000;
     * @return 000000000000000000000000092378948631305 1642495488597 0741    39+13+4=56
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:9997
     *        冲突数:3
     */
    public static String serialMode3() {
        NetworkInterface netInterface;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac =new BigInteger(netInterface.getHardwareAddress()).longValue();
        }catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String str = String.format("%039d%013d%04d",mac,System.currentTimeMillis(),(int)(Math.random()*1000));
        return str;
    }

===========================采用MAC地址+yyyyMMddHHmmssSSS+Math.Random()*1000;

    /**
     * 流水号 60位 
     *     采用MAC地址+yyyyMMddHHmmssSSS+Math.Random()*1000;
     * @return 000000000000000000000000092378948631305 20220118164448730 0017  39+17+4=60
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:9999
     *        冲突数:1
     */
    public static String serialMode4() {
        NetworkInterface netInterface;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac =new BigInteger(netInterface.getHardwareAddress()).longValue();
        }catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String str = String.format("%039d%s%04d",mac,DateFormatUtil.formatCurDate(DateFormatUtil.DATE_PATTERN_yyyyMMddHHmmssSSS),(int)(Math.random()*1000));
        return str;
    }

===========================采用MAC地址+PID+System.currentTimeMillis();休眠10ms;

    /**
     * 流水号 57位 
     *     采用MAC地址+PID+System.currentTimeMillis();休眠10ms;
     * @return 000000000000000000000000092378948631305 15008 1642495488873    39+5+13=57
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:7574
     *        冲突数:2426
     */
    public static String serialMode5() {
        NetworkInterface netInterface;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac =new BigInteger(netInterface.getHardwareAddress()).longValue();
        }catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String name = ManagementFactory.getRuntimeMXBean().getName();
        // get pid 线程号
        String pid = name.split("@")[0];
        String str = String.format("%039d%s%013d",mac,pid,System.currentTimeMillis());
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return str;
    }

===========================采用MAC地址+PID+yyyyMMddHHmmssSSS;休眠10ms;

    /**
     * 流水号 61位 
     *     采用MAC地址+PID+yyyyMMddHHmmssSSS;休眠10ms;
     * @return 000000000000000000000000092378948631305 15008 20220118164449000  39+5+17=61
     *     备注:测试线程池容量为100,线程数为10000 
     *          总个数:10000
     *        去重总个数:7100
     *        冲突数:2900
     */
    public static String serialMode6() {
        NetworkInterface netInterface;
        long mac=0;
        try {
            netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            mac =new BigInteger(netInterface.getHardwareAddress()).longValue();
        }catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
        String name = ManagementFactory.getRuntimeMXBean().getName();
        // get pid 线程号
        String pid = name.split("@")[0];
        String str = String.format("%039d%s%s",mac,pid,DateFormatUtil.formatCurDate(DateFormatUtil.DATE_PATTERN_yyyyMMddHHmmssSSS));
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return str;
    }

===========================单机环境下唯一流水号生成方式,单线程测试类

    @Test
    public void test_serialMode() {
        System.out.println(SerialUtil.serialMode1());
        System.out.println(SerialUtil.serialMode2());
        System.out.println(SerialUtil.serialMode3());
        System.out.println(SerialUtil.serialMode4());
        System.out.println(SerialUtil.serialMode5());
        System.out.println(SerialUtil.serialMode6());
    }

===========================单机环境下唯一流水号生成方式,多线程测试类

import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SerialImplThread extends Thread {
    
    protected static final Logger logger = LoggerFactory.getLogger(SerialImplThread.class);
    
    public static int ID = 0;
    public static Set tries = new HashSet();
    static Object lock = new Object();
    
    public void run(){
//        String str = GlobalLockWorker.getSerialNumber1(); //单机场景下不重复流水号
//        String str = GlobalLockWorker.getSerialNumber2(); //单机场景下不重复流水号
//        String str = SerialUtil.serialMode1(); //单机场景下不重复流水号
//        String str = SerialUtil.serialMode2(); //单机场景下不重复流水号
//        String str = SerialUtil.serialMode3(); //单机场景下有些许重复流水号  总个数:10000  去重总个数:9997 冲突数:3
//        String str = SerialUtil.serialMode4(); //单机场景下有些许重复流水号  总个数:10000  去重总个数:9999 冲突数:1
//        String str = SerialUtil.serialMode5(); //单机场景下有部分重复流水号  总个数:10000  去重总个数:7574 冲突数:2426
        String str = SerialUtil.serialMode6(); //单机场景下有部分重复流水号  总个数:10000  去重总个数:7100 冲突数:2900
        synchronized (lock) {
            tries.add(str);
            ID++;
            logger.info("流水号生成个数={}", ID);
        }
    }
}

===========================单机环境下唯一流水号生成方式,多线程执行测试类

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Test;

public class SerialImplThreadTest {

    @SuppressWarnings("static-access")
    @Test
    public void test_serialMod() {
        int n =100;
        ExecutorService execupool = Executors.newFixedThreadPool(n);
        for(int i=0;i<10000;i++){
            Thread serial = new SerialImplThread();
            execupool.execute(serial);
        }
        execupool.shutdown();
        while(!execupool.isTerminated()){ }
        SerialImplThread serialImplThread = new SerialImplThread();
        System.out.println("总个数:" + (serialImplThread.ID));
        System.out.println("去重总个数:" + (serialImplThread.tries.size()));
        System.out.println("冲突数:" + (serialImplThread.ID - serialImplThread.tries.size()));
    }
}

相关