Twitter的雪花算法(JAVA)

直接上代碼(這段代碼我是抄的,雖然看不懂,可是我知道能用,哈哈)算法

package com.example.learning.雪花算法;

/**
 * 描述:雪花算法
 * User: 曾遠征
 * Date: 2018-09-17
 * Time: 15:51
 */
public class SnowflakesTools {

    //開始時間截 (2015-01-01)
    private final long twepoch = 1489111610226L;
    //機器ID所佔位置
    private final long workerIdBits = 5L;
    //數據標識所佔位數
    private final long datacenterIdBits = 5L;
    //支持的最大機器id,結果是31 (這個移位算法能夠很快的計算出幾位二進制數所能表示的最大十進制數)
    private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
    //支持的最大數據標識id,結果是31
    private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    //序列在id中佔的位數
    private final long sequenceBits = 12L;
    //機器ID向左移12位
    private final long workerIdShift = sequenceBits;
    //數據標識id向左移17位(12+5)
    private final long datacenterIdShift = sequenceBits + workerIdBits;
    //時間截向左移22位(5+5+12)
    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    //生成序列的掩碼,這裏爲4095 (0b111111111111=0xfff=4095)
    private final long sequenceMask = -1L ^ (-1L << sequenceBits);

    //工做機器ID(0~31)
    private long workerId;

    //數據中心ID(0~31)
    private long datacenterId;

    //毫秒內序列(0~4095)
    private long sequence = 0L;

    //上次生成ID的時間截
    private long lastTimestamp = -1L;

    public SnowflakesTools(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;
    }

    /**
     * 得到下一個ID (該方法是線程安全的)
     * @return
     */
    public synchronized long nextId() {
        long timestamp = timeGen();

        //若是當前時間小於上一次ID生成的時間戳,說明系統時鐘回退過這個時候應當拋出異常
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(String.format(
                    "Clock moved backwards.  Refusing to generate id for %d milliseconds", 
                    lastTimestamp - timestamp));
        }

        //若是是同一時間生成的,則進行毫秒內序列
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            //毫秒內序列溢出
            if (sequence == 0) {
                //阻塞到下一個毫秒,得到新的時間戳
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {//時間戳改變,毫秒內序列重置
            sequence = 0L;
        }

        //上次生成ID的時間截
        lastTimestamp = timestamp;

        //移位並經過或運算拼到一塊兒組成64位的ID
        return ((timestamp - twepoch) << timestampLeftShift)
                | (datacenterId << datacenterIdShift)
                | (workerId << workerIdShift)
                | sequence;
    }

    /**
     * 阻塞到下一個毫秒,直到得到新的時間戳
     * @param lastTimestamp
     * @return
     */
    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    /**
     * 返回以毫秒爲單位的當前時間
     * @return
     */
    protected long timeGen() {
        return System.currentTimeMillis();
    }

    public static void main(String[] args) {
        SnowflakesTools idWorker = new SnowflakesTools(0, 0);
        for (int i = 0; i < 1000; i++) {
            long id = idWorker.nextId();
            System.out.println(id);
        }
    }

}
相關文章
相關標籤/搜索