c#实现分组服务器,单一无重复生成ID

class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             List<Thread> threads = new List<Thread>();
 6             getID();
 7             Console.WriteLine("重复次数:" + ccount);
 8             Console.ReadLine();
 9         }
10 
11         public static void getID()
12         {
13             for (int j = 0; j < 20; j++)
14             {
15                 for (int i = 0; i < 99999; i++)
16                 {
17                     // GetDate();
18                     Console.WriteLine(GetDate());
19                 }
20                 Thread.Sleep(980);
21             }
22         }
23 
24         static int serverID = 99;
25         static ulong ID = 1;
26         static HashSet<string> longset = new HashSet<string>();
27         static int ccount = 0;
28 
29         public static string GetDate()
30         {
31             lock (typeof(ulong))
32             {
33                 if (ID + 1 > 99999)
34                 {
35                     ID = 0;
36                 }
37                 ID++;
38                 string _ulong = serverID + "" + System.DateTime.Now.ToString("yyMMddHHmmss") + ("" + ID).PadLeft(5, '0');
39                 if (longset.Contains(_ulong))
40                 {
41                     ccount++;
42                 }
43                 longset.Add(_ulong);
44                 return _ulong;
45             }
46         }
47     }
复制代码

该程序用于生成的ID太强规则可言的,可实现分布式生成ID,在合并数据是无重复ID~!目前支持99组服务器同时运行,每秒9999个ID生成~!

 

http://www.cnblogs.com/ty408/p/4326419.html

posted @ 2017-07-26 10:47  左正  阅读(318)  评论(0编辑  收藏  举报