C# 同一时间批量生成订单号不重复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetOrderNumbers("订单号",1000);
            Console.WriteLine();
            Console.ReadKey();
        }

        private static object locker = new object();
        private static int nownum = 0;
        /// <summary>
        /// 批量生成订单
        /// </summary>
        /// <param name="c">前缀</param>
        /// <param name="nums">生成的订单数量</param>
        public static void GetOrderNumbers(string c,int nums)
        {       

            for(int i=0;i< nums; i++)
            {
                lock (locker)
                {
                    if (nownum == int.MaxValue)
                        nownum = 0;
                    else
                        nownum++;

                    string t= c + DateTime.Now.ToString("yyyyMMddHHmmss") + nownum.ToString().PadLeft(10, '0');
                    Console.WriteLine(t);
                }
           
            }
           
        }
    }

}

 

posted on 2016-12-20 16:42  嘿呀土豆  阅读(1797)  评论(0编辑  收藏  举报

导航