• 生成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();
    /// <summary>
    /// 生成18位不重复的ID号
    /// </summary>
    /// <returns></returns>
    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;
    }
    }
    /// <summary>
    /// 测试函数
    /// </summary>
    /// <returns></returns>
    public bool Execute()
    {
    Int64 tempValue = CreateID();
    return true;
    }
    /// <summary>
    /// 得到3位随机数
    /// </summary>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 检测是否有重复的随机数
    /// </summary>
    /// <param name="arrNum"></param>
    /// <param name="tmp"></param>
    /// <param name="minValue"></param>
    /// <param name="maxValue"></param>
    /// <param name="ra"></param>
    /// <returns></returns>
    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
    {
    /// <summary> 
    /// 并发测试 
    /// </summary> 
    public class ConcurrentTest : IDisposable
    {
    #region 私有方法
    /// <summary> 
    /// 测试方法所在的接口 
    /// </summary> 
    private Func<bool> func;
    /// <summary> 
    /// 主线程控制信号 
    /// </summary> 
    private ManualResetEvent manualResetEvent;
    /// <summary> 
    /// 测试线程控制信号 
    /// </summary> 
    private ManualResetEvent threadResetEvent;
    /// <summary> 
    /// 待执行的线程数 
    /// </summary> 
    private List<int> threads;
    /// <summary> 
    /// 测试结果 
    /// </summary> 
    private List<ConcurrentTestResult> results;
    /// <summary> 
    /// 执行测试的成功数 
    /// </summary> 
    private int successCount;
    /// <summary> 
    /// 执行测试的失败数 
    /// </summary> 
    private int failureCount;
    /// <summary> 
    /// 测试耗时 
    /// </summary> 
    private long elapsedMilliseconds;
    /// <summary> 
    /// 当前线程 
    /// </summary> 
    private int currentIndex;
    /// <summary> 
    /// 当前测试的总线程数 
    /// </summary> 
    private int currentCount;
    /// <summary> 
    /// 思考时间 
    /// </summary> 
    private int thinkTime;
    /// <summary> 
    /// 重复次数 
    /// </summary> 
    private int repeatCount;
    /// <summary> 
    /// 测试计时器 
    /// </summary> 
    private Stopwatch stopwatch;
    #endregion
    #region 构造函数
    /// <summary> 
    /// 构造函数 
    /// </summary> 
    public ConcurrentTest()
    {
    manualResetEvent = new ManualResetEvent(true);
    threadResetEvent = new ManualResetEvent(true);
    stopwatch = new Stopwatch();
    }
    #endregion
    #region 执行测试
    /// <summary> 
    /// 执行多线程测试 
    /// </summary> 
    /// <param name="threadCount">需要测试的线程数</param> 
    /// <param name="func">待执行方法</param> 
    /// <returns></returns> 
    public List<ConcurrentTestResult> Execute(int threadCount, Func<bool> func)
    {
    return Execute(threadCount, 1, func);
    }
    /// <summary> 
    /// 执行多线程测试 
    /// </summary> 
    /// <param name="threadCount">需要测试的线程数</param> 
    /// <param name="repeatCount">重复次数</param> 
    /// <param name="func">待执行方法</param> 
    /// <returns></returns> 
    public List<ConcurrentTestResult> Execute(int threadCount, int repeatCount, Func<bool> func)
    {
    return Execute(threadCount, 0, repeatCount, func);
    }
    /// <summary> 
    /// 执行多线程测试 
    /// </summary> 
    /// <param name="threadCount">需要测试的线程数</param> 
    /// <param name="thinkTime">思考时间,单位耗秒</param> 
    /// <param name="repeatCount">重复次数</param> 
    /// <param name="func">待执行方法</param> 
    /// <returns></returns> 
    public List<ConcurrentTestResult> Execute(int threadCount, int thinkTime, int repeatCount, Func<bool> func)
    {
    return Execute(new List<int>() { threadCount }, thinkTime, repeatCount, func);
    }
    /// <summary> 
    /// 执行多线程测试 
    /// </summary> 
    /// <param name="threads">分别需要测试的线程数</param> 
    /// <param name="thinkTime">思考时间,单位耗秒</param> 
    /// <param name="repeatCount">重复次数</param> 
    /// <param name="func">待执行方法</param> 
    /// <returns></returns> 
    public List<ConcurrentTestResult> 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 验证参数
    /// <summary> 
    /// 验证参数 
    /// </summary> 
    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 创建多线程并执行测试
    /// <summary> 
    /// 创建多线程进行测试 
    /// </summary> 
    private void CreateMultiThread()
    {
    results = new List<ConcurrentTestResult>(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);
    }
    }
    }
    /// <summary> 
    /// 执行测试方法 
    /// </summary> 
    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 释放资源
    /// <summary> 
    /// 释放资源 
    /// </summary> 
    public void Dispose()
    {
    manualResetEvent.Close();
    threadResetEvent.Close();
    }
    #endregion
    }
    /// <summary> 
    /// 并发测试结果 
    /// </summary> 
    public class ConcurrentTestResult
    {
    /// <summary> 
    /// 当前执行线程总数 
    /// </summary> 
    public int ThreadCount
    {
    get { return SuccessCount + FailureCount; }
    }
    /// <summary> 
    /// 测试成功数 
    /// </summary> 
    public int SuccessCount { get; set; }
    /// <summary> 
    /// 测试失败数 
    /// </summary> 
    public int FailureCount { get; set; }
    /// <summary> 
    /// 总耗时 
    /// </summary> 
    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);
    }
    }
    源码下载:下载

  • 相关阅读:
    中国剩余定理及拓展
    20191128-1 总结
    获取动态图
    弹球游戏设计
    作业要求 20191121-1 每周例行报告
    作业要求 20191114-1 每周例行报告
    对现组内成员的感谢
    作业要求 20191107-1 每周例行报告
    20191031-1 每周例行报告
    作业要求 20191024-1每周例行报告
  • 原文地址:https://www.cnblogs.com/HeroBeast/p/2121576.html
Copyright © 2020-2023  润新知