c#实现分组服务器,单一无重复生成ID
生活随笔
收集整理的這篇文章主要介紹了
c#实现分组服务器,单一无重复生成ID
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Program2 {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
總結
以上是生活随笔為你收集整理的c#实现分组服务器,单一无重复生成ID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多线程系列一
- 下一篇: C# Socket系列一 简单的创建so