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

 1 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生成~!

时间: 2024-08-27 14:26:28

c#实现分组服务器,单一无重复生成ID的相关文章

使用C++生成1-33中的6个随机数,无重复

生成1-33中的6个随机数,无重复 ------------------------------------------------------------------------ 方法1.每生成一个随机数,便于前面的所有随机数进行比较,如果有重复,则舍去不要,重新选取. 但该方法十分费时,并且在数据量巨大的并且有一定限制的时候,会引发巨大问题. 例如要生成10000个随机数,范围是0-9999,且不能重复,那么最后几个随机数有可能需要相当长的时间才能筛选出来. 方法2. 下面我们从另外一个角度

ssh实现服务器间无密钥通信

通过ssh-keygent实现两台服务器间的无密钥通信: 两台服务器均为centos6.6系统: [[email protected] ~]# ssh-keygen -t rsa   #全部按enter键,不需要输入其它内容 Generating public/private rsa key pair. Enter file in which to save the key(/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase

[LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

leetcode无重复字符的最长字串 python实现

无重复字符的最长字串是一道字符串处理算法的题目,在日常编程中,处理字符串是常见任务.用Python来实现leetcode这道算法题,该题目会涉及到一个概念"滑动窗口". 一.题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度(Longest substring without repeating characters). 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所

Leetcode——3. 无重复字符的最长子串

难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一个字符串,请你找出其中不含有重复字符的?最长子串?的长度. 示例?1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字

求全排列(数组有重复元素和数组无重复元素) 回溯 递归

http://www.cnblogs.com/TenosDoIt/p/3662644.html 无重复元素 http://blog.csdn.net/havenoidea/article/details/12838479 有重复元素

[LeetCode] Longest substring without repeating characters 最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数?并把他们都输出。

/** * 有1.2.3.4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数?并把他们都输出. * */ public class Test1 { public static void main(String[] args) { int num = 0, c = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { for (int k = 1; k <= 4; k++) { if (i != j &

字符串练习(八):最长无重复字符子串

对于一个字符串,请设计一个高效算法,找到字符串的最长无重复字符的子串长度. 给定一个字符串A及它的长度n,请返回它的最长无重复字符子串长度.保证A中字符全部为小写英文字符,且长度小于等于500. 测试样例: "aabcb",5 返回:3 public class DistinctSubstring { public int longestSubstring(String A, int n) { // write code here if(A==null || n==0){ return