91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Snowflak算法 (C#版本)

發布時間:2020-08-03 15:14:25 來源:網絡 閱讀:1002 作者:龍寶寶之光 欄目:編程語言

最近看了twitter的分布式自增ID算法Snowflak,自己根據開源的代碼寫了一個C#的版本,以記錄學習。 twitter的開源項目地址為:https://github.com/twitter/snowflake 用Scala實現


class IdWorker
    {
        //機器標識位數
        private const int WORKER_ID_BITS = 4;
        //機器標識位的最大值
        private const long MAX_WORKER_ID = -1L ^ -1L << WORKER_ID_BITS;
        //毫秒內自增位
        private const int SEQUENCE_BITS = 10;
        //自增位最大值
        private const long SEQUENCE_Mask = -1L ^ -1L << SEQUENCE_BITS;
        private const long twepoch = 1398049504651L;
        //時間毫秒值向左偏移位
        private const int timestampLeftShift = SEQUENCE_BITS + WORKER_ID_BITS;
        //機器標識位向左偏移位
        private const int WORKER_ID_SHIFT = SEQUENCE_BITS;
        private static readonly DateTime START_TIME = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
        private static readonly object LOCK = new object();
        private long sequence = 0L;
        private long lastTimestamp = -1L;
        private long workerId;
        public IdWorker(long workerId)
        {
            if (workerId > MAX_WORKER_ID || workerId < 0)
            {
                throw new ArgumentException(string.Format("worker id can't be greater than {0} or less than 0", MAX_WORKER_ID));
            }
            this.workerId = workerId;
        }
        /// <summary>
        /// 獲取下一個id值
        /// </summary>
        /// <returns></returns>
        public long NextId()
        {
            lock (LOCK)
            {
                long timestamp = TimeGen();
                //當前時間小于上一次時間,錯誤
                if (timestamp < this.lastTimestamp)
                {
                    throw new Exception(string.Format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
                }
                //當前毫秒內
                if (this.lastTimestamp == timestamp)
                {
                    //+1 求余
                    this.sequence = (this.sequence + 1) & SEQUENCE_Mask;
                    //當前毫秒內計數滿了,等待下一秒
                    if (this.sequence == 0)
                    {
                        timestamp = tilNextMillis(lastTimestamp);
                    }
                }
                else      //不是當前毫秒內
                {
                    this.sequence = 0;   //重置當前毫秒計數
                }
                this.lastTimestamp = timestamp;
                //當前毫秒值 | 機器標識值 | 當前毫秒內自增值
                long nextId = ((timestamp - twepoch << timestampLeftShift))
                | (this.workerId << WORKER_ID_SHIFT) | (this.sequence);
                return nextId;
            }
        }
        /// <summary>
        /// 等待下一個毫秒
        /// </summary>
        /// <param name="lastTimestamp"></param>
        /// <returns></returns>
        private long tilNextMillis(long lastTimestamp)
        {
            long timestamp = TimeGen();
            while (timestamp <= lastTimestamp)
            {
                timestamp = TimeGen();
            }
            return timestamp;
        }
        /// <summary>
        /// 獲取當前時間的Unix時間戳
        /// </summary>
        /// <returns></returns>
        private long TimeGen()
        {
            return (DateTime.UtcNow.Ticks - START_TIME.Ticks) / 10000;
        }
    }


調用方式如下

static void Main(string[] args)
{
    IdWorker worker = new IdWorker(12L);
    for (int i = 0; i < 100000; i++)
    {
        Console.WriteLine(worker.NextId());
    }
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

吐鲁番市| 镇巴县| 清丰县| 古交市| 喀喇沁旗| 延安市| 绥江县| 镇巴县| 富民县| 恩平市| 仪征市| 游戏| 三门县| 泾川县| 泰兴市| 县级市| 连城县| 弥渡县| 五大连池市| 高青县| 大邑县| 凉山| 乌拉特前旗| 兴文县| 云龙县| 环江| 泾源县| 白银市| 阿坝| 民丰县| 三亚市| 大英县| 全州县| 婺源县| 陵川县| 安康市| 遂川县| 涿鹿县| 修武县| 宣威市| 大庆市|