码迷,mamicode.com
首页 > 编程语言 > 详细

C#线程安全的静态变量

时间:2015-09-26 00:34:31      阅读:511      评论:0      收藏:0      [点我收藏+]

标签:

生成唯一号:

/// <summary>
    /// 唯一码
    /// </summary>
    public class UniqueNumber
    {
        private static long num = 0;//流水号
        private static object lockObj = new object();
        /// <summary>
        /// 生成自增长码
        /// </summary>
        /// <returns></returns>
        static public long GenerateUniqueNumber()//应为private,暂时为public用来测试。
        {
            lock (lockObj)
            {
                num = num + 1;
                num = (num == 100000 ? 1 : num);
                num = long.Parse(num.ToString().PadLeft(5, 0));//五位(99999)
            }
            return num;
        }
        /// <summary>
        /// 获取唯一码
        /// </summary>
        /// <param name="SystemNo">系统号</param>
        /// <returns>唯一码</returns>
        static public long GetUniqueNumber(int SystemNo)
        {
            if (SystemNo > 99 && SystemNo < 1)
            {
                throw new Exception("系统号有误");
            }
            lock (lockObj)
            {
                //long utc = DateTime.Now.ToFileTimeUtc() - (new DateTime(2015, 1, 1)).ToFileTimeUtc();//15位有点长,
                string time =
                  DateTime.Now.Year.ToString().Substring(2) +
                  DateTime.Now.Month.ToString() +
                  DateTime.Now.Day.ToString() +
                  DateTime.Now.Hour.ToString() +
                  DateTime.Now.Minute.ToString() +
                  DateTime.Now.Second.ToString();//12位;
                return long.Parse(time + GenerateUniqueNumber().ToString() + SystemNo.ToString());//19位
            }
        }
    }

单元测试:

 /// <summary>
    /// 唯一码测试
    /// </summary>
    [TestClass]
    public class UniqueNumberTest
    {
        static List<long> list = new List<long>();
        
        /// <summary>
        /// 并发测试唯一码
        /// </summary>
        [TestMethod]
        public void GetUniqueNumberTest()
        {
            for (int i = 1; i < 99; i++)//N个线程
            {
                Thread thread = new Thread(new ParameterizedThreadStart(Execution));
                thread.Start(i);
            }
        }
        /// <summary>
        /// 错误事例:并发测试唯一码,有重复数
        /// </summary>
        [TestMethod]
        public void ERRORGetUniqueNumberTest()
        {
            for (int i = 1; i < 99; i++)//N个线程
            {
                Thread thread = new Thread(ERRORExecution);
                thread.Start();
            }
        }
        /// <summary>
        /// 根据系统号获取唯一码
        /// </summary>
        /// <param name="sysNo">系统号</param>
        public static void Execution(object sysNo)
        {
            for (int i = 0; i < 1000; i++)//M次循环执行
            {
                long v = UniqueNumber.GetUniqueNumber((int)sysNo);
                if (!list.Contains(v))//如果列表不包括就添加
                {
                    list.Add(v);
                }
                else
                {
                    Console.WriteLine("错误:" + v);//已经包括说明有重复值,就打印出来
                }
            }
        }
        public static void ERRORExecution()
        {
            for (int i = 0; i < 1000; i++)//M次循环执行
            {
                long v = UniqueNumber.GenerateUniqueNumber();
                if (!list.Contains(v))//如果列表不包括就添加
                {
                    list.Add(v);
                }
                else
                {
                    Console.WriteLine("错误:" + v);//已经包括说明有重复值,就打印出来
                }
            }
        }
    }

 

C#线程安全的静态变量

标签:

原文地址:http://www.cnblogs.com/zhangzhi19861216/p/4839813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!