上次簡單的說一下:http://www.cnblogs.com/dunitian/p/6041745.html#uidhtml
C#版本的國外朋友已經封裝了,你們能夠去看看:https://github.com/ccollie/snowflake-netgit
強大的網友出來個簡化版本:http://blog.csdn.net/***/article/details/*** (地址我就不貼了,對前輩須要最起碼的尊敬)github
一開始我用的是這個簡化版本,後來發現有重複項。。。(demo:https://github.com/dunitian/TempCode/tree/master/2016-11-16/Twitter_Snowflake)併發
全局ID的激烈討論:https://q.cnblogs.com/q/53552/測試
以後在外國大牛的基礎上重寫修改了部份內容(https://github.com/ccollie/snowflake-net),添加了一些註解等【支持Core】。如今是能夠去Nuget直接下載使用的:Snowflake.Netui
源碼地址:https://github.com/dunitian/snowflake-netspa
測試用例:.net
測試代碼: 3d
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Snowflake.Net; namespace Snowflake.ZConsole { class Program { private static int N = 2000000; private static HashSet<long> set = new HashSet<long>(); private static IdWorker worker = new IdWorker(1, 1); private static int taskCount = 0; static void Main(string[] args) { Task.Run(() => GetID()); Task.Run(() => GetID()); Task.Run(() => GetID()); Task.Run(() => Printf()); Console.ReadKey(); } private static void Printf() { while (taskCount != 3) { Console.WriteLine("..."); Thread.Sleep(1000); } Console.WriteLine(set.Count == N * taskCount); } private static object o = new object(); private static void GetID() { for (var i = 0; i < N; i++) { var id = worker.NextId(); lock (o) { if (set.Contains(id)) { Console.WriteLine("發現重複項 : {0}", id); } else { set.Add(id); } } } Console.WriteLine($"任務{++taskCount}完成"); } } }
可能有些人只關心之後怎麼用?==》htm
IdWorker worker = new IdWorker(1, 1); //大併發的狀況下,減小new的次數能夠有效避免重複的可能
var id = worker.NextId();
有可能上面的減小new有些同志不太懂,(⊙o⊙)…,舉個例子:
測試代碼不變的狀況下,改這麼一句:
完整調用demo:(https://github.com/dunitian/snowflake-net/tree/master/Demo)
core:(https://github.com/dunitian/snowflake-net/tree/master/Demo.Core)
IdWorker.Init().NextId()