生成18位不重复的ID

思路:前15位是有时间串组成,后三位是由3位随机数组成:yyyyMMddHHmmssf+xxx

生成随机数的类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace WindowsFormsApplication5
{
public class IDTest
{
private static readonly object SequenceLock = new object();
/// 
/// 生成18位不重复的ID号
/// 
/// 
public Int64 CreateID()
{
lock (SequenceLock)
{
Int64 id = -1;
Random random = new Random();
int d = GetNumber();
string strTimeSpan = DateTime.Now.ToString("yyyyMMddHHmmssf") + d.ToString();
Trace.WriteLine("createID="+strTimeSpan);
Int64.TryParse(strTimeSpan, out id);
return id;
}
}
/// 
/// 测试函数
/// 
/// 
public bool Execute()
{
Int64 tempValue = CreateID();
return true;
}
/// 
/// 得到3位随机数
/// 
/// 
public int GetNumber()
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int[] arrNum = new int[10];
int tmp = 0;
int minValue = 100;
int maxValue = 999;
for (int i = 0; i < 999; i++)
{
tmp = ra.Next(minValue, maxValue); //随机取数
tmp = getNum(arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
}
return tmp;
}
/// 
/// 检测是否有重复的随机数
/// 
/// 
/// 
/// 
/// 
/// 
/// 
public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(minValue, maxValue); //重新随机获取。 
getNum(arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
}
n++;
}
return tmp;
}
}
}
使用了园子里的多线程测试类,原文:
http://www.cnblogs.com/moozi/archive/2011/04/16/2018435.html
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication5
{
///  
/// 并发测试 
///  
public class ConcurrentTest : IDisposable
{
#region 私有方法
///  
/// 测试方法所在的接口 
///  
private Func<bool> func;
///  
/// 主线程控制信号 
///  
private ManualResetEvent manualResetEvent;
///  
/// 测试线程控制信号 
///  
private ManualResetEvent threadResetEvent;
///  
/// 待执行的线程数 
///  
private List<int> threads;
///  
/// 测试结果 
///  
private List results;
///  
/// 执行测试的成功数 
///  
private int successCount;
///  
/// 执行测试的失败数 
///  
private int failureCount;
///  
/// 测试耗时 
///  
private long elapsedMilliseconds;
///  
/// 当前线程 
///  
private int currentIndex;
///  
/// 当前测试的总线程数 
///  
private int currentCount;
///  
/// 思考时间 
///  
private int thinkTime;
///  
/// 重复次数 
///  
private int repeatCount;
///  
/// 测试计时器 
///  
private Stopwatch stopwatch;
#endregion
#region 构造函数
///  
/// 构造函数 
///  
public ConcurrentTest()
{
manualResetEvent = new ManualResetEvent(true);
threadResetEvent = new ManualResetEvent(true);
stopwatch = new Stopwatch();
}
#endregion
#region 执行测试
///  
/// 执行多线程测试 
///  
/// 需要测试的线程数 
/// 待执行方法 
///  
public List Execute(int threadCount, Func<bool> func)
{
return Execute(threadCount, 1, func);
}
///  
/// 执行多线程测试 
///  
/// 需要测试的线程数 
/// 重复次数 
/// 待执行方法 
///  
public List Execute(int threadCount, int repeatCount, Func<bool> func)
{
return Execute(threadCount, 0, repeatCount, func);
}
///  
/// 执行多线程测试 
///  
/// 需要测试的线程数 
/// 思考时间,单位耗秒 
/// 重复次数 
/// 待执行方法 
///  
public List Execute(int threadCount, int thinkTime, int repeatCount, Func<bool> func)
{
return Execute(new List<int>() { threadCount }, thinkTime, repeatCount, func);
}
///  
/// 执行多线程测试 
///  
/// 分别需要测试的线程数 
/// 思考时间,单位耗秒 
/// 重复次数 
/// 待执行方法 
///  
public List Execute(List<int> threads, int thinkTime, int repeatCount, Func<bool> func)
{
this.func = func;
this.threads = threads;
this.thinkTime = thinkTime;
this.repeatCount = repeatCount;
CheckParameters();
CreateMultiThread();
return this.results;
}
#endregion
#region 验证参数
///  
/// 验证参数 
///  
private void CheckParameters()
{
if (func == null) throw new ArgumentNullException("func不能为空");
if (threads == null || threads.Count == 0) throw new ArgumentNullException("threads不能为空或者长度不能为0");
if (thinkTime < 0) throw new Exception("thinkTime不能小于0");
if (repeatCount <= 0) throw new Exception("repeatCount不能小于等于0");
}
#endregion
#region 创建多线程并执行测试
///  
/// 创建多线程进行测试 
///  
private void CreateMultiThread()
{
results = new List(threads.Count);
foreach (int threadCount in threads)
{
for (int repeat = 0; repeat < repeatCount; repeat++)
{
//主线程进入阻止状态 
manualResetEvent.Reset();
//测试线程进入阻止状态 
threadResetEvent.Reset();
stopwatch.Reset();
currentCount = threadCount;
currentIndex = 0;
successCount = 0;
failureCount = 0;
elapsedMilliseconds = 0;
for (int i = 0; i < currentCount; i++)
{
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();
}
//阻止主线程,等待测试线程完成测试 
manualResetEvent.WaitOne();
results.Add(new ConcurrentTestResult()
{
FailureCount = failureCount,
SuccessCount = successCount,
ElapsedMilliseconds = elapsedMilliseconds
});
Thread.Sleep(thinkTime);
}
}
}
///  
/// 执行测试方法 
///  
private void DoWork()
{
bool executeResult;
Interlocked.Increment(ref currentIndex);
if (currentIndex < currentCount)
{
//等待所有线程创建完毕后同时执行测试 
threadResetEvent.WaitOne();
}
else
{
//最后一个线程创建完成,通知所有线程,开始执行测试 
threadResetEvent.Set();
//开始计时 
stopwatch.Start();
}
//执行测试 
executeResult = func();
Interlocked.Decrement(ref currentIndex);
if (currentIndex == 0)
{
//最后一个线程执行的测试结束,结束计时 
stopwatch.Stop();
elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
//保存测试结果 
if (executeResult)
Interlocked.Increment(ref successCount);
else
Interlocked.Increment(ref failureCount);
//通知主线程继续 
manualResetEvent.Set();
}
else
{
//保存测试结果 
if (executeResult)
Interlocked.Increment(ref successCount);
else
Interlocked.Increment(ref failureCount);
}
}
#endregion
#region 释放资源
///  
/// 释放资源 
///  
public void Dispose()
{
manualResetEvent.Close();
threadResetEvent.Close();
}
#endregion
}
///  
/// 并发测试结果 
///  
public class ConcurrentTestResult
{
///  
/// 当前执行线程总数 
///  
public int ThreadCount
{
get { return SuccessCount + FailureCount; }
}
///  
/// 测试成功数 
///  
public int SuccessCount { get; set; }
///  
/// 测试失败数 
///  
public int FailureCount { get; set; }
///  
/// 总耗时 
///  
public long ElapsedMilliseconds { get; set; }
}
}

测试方法:

 

 using (ConcurrentTest concurrentTest = new ConcurrentTest())
{
var result = concurrentTest.Execute(2, new IDTest().Execute);
foreach (var item in result)
{
Console.WriteLine("线程数:{0}\t成功:{1}\t失败:{2}\t耗时:{3}",
item.ThreadCount, item.SuccessCount, item.FailureCount, item.ElapsedMilliseconds);
}
}
源码下载:下载

转载于:https://www.cnblogs.com/HeroBeast/archive/2011/07/29/2121576.html

你可能感兴趣的:(生成18位不重复的ID)