id生成器

注: 虽然来源于其它人, 但有改动, 原版不能实现无重复。 

/***********
 * http://www.cnblogs.com/smark/p/3453528.html
 ***********/

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


namespace UID
{
    public class UIDCreater
    {
        private static DateTime mDefaultTime = new DateTime(2018, 1, 1);

        public UIDCreater()
        {
            StartValue = 1;
            Step = 1;
            mLastSecond = Convert.ToInt64((DateTime.Now - mDefaultTime).TotalMilliseconds);

            Sequence = 1;

        }

        public UIDCreater(uint start, uint step)
        {
            StartValue = start;
            Step = step;
            Sequence = (ushort)start;
        }

        private long mLastSecond;

        private ushort Sequence;

        private static readonly Object StaticLockObj = new object();

        public virtual long GetValue()
        {
            lock (StaticLockObj)
            {
                long result;
                long second = Convert.ToInt64((DateTime.Now - mDefaultTime).TotalMilliseconds);
                if (second > mLastSecond)
                {
                    Sequence = (ushort)StartValue;
                    mLastSecond = second;
                }
                result = (second << 16) | (long)Sequence;
                Sequence += (ushort)Step;
                return result;
            }
        }

        public virtual long GetValue(int years)
        {
            lock (this)
            {
                long result;
                long second = Convert.ToInt64((DateTime.Now.AddYears(years) - mDefaultTime).TotalMilliseconds);
                mLastSecond = Convert.ToInt64((DateTime.Now.AddYears(years) - mDefaultTime).TotalMilliseconds);
                if (second > mLastSecond)
                {
                    Sequence = (ushort)StartValue;
                    mLastSecond = second;
                }
                result = (second << 17) | (long)Sequence;
                Sequence += (ushort)Step;
                return result;
            }
        }

        public Byte[] GetValueData()
        {
            return BitConverter.GetBytes(GetValue());
        }

        public uint StartValue
        {
            get;
            set;
        }

        public uint Step
        {
            get;
            set;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace UID
{
    public class Creater
    {
        private static UIDCreater _uidCreater;
        private static object Singleton_Lock = new object();

        private static UIDCreater uidCreater
        {
            get
            {
                if (_uidCreater == null)
                {
                    lock (Singleton_Lock)
                    {
                        if (_uidCreater == null)
                        {
                            _uidCreater = new UIDCreater();
                        }
                    }
                }

                return _uidCreater;
            }
        }

        public static long GetId()
        {
            return uidCreater.GetValue();
        }

        public static long GetId(int years)
        {
            return uidCreater.GetValue(years);
        }

        public static UIDCreater Current
        {
            get { return uidCreater; }
        }
    }
}

测试代码:

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

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 1000;
            CreateId1(num);
            Console.Read();
        }

        private static void CreateId1(int num)
        {
            List<TestItem> list = new List<TestItem>();
            for (int i = 0; i < num; i++)
                list.Add(new TestItem() { Id = i, Ticks = 0 });

            Parallel.For(0, num, (item) =>
            {
                list.Find(p => p.Id == item).Ticks = Creater.GetId();
            });
            var r = list.Where(p=>p.Ticks != 0).GroupBy(p => p.Ticks);
            Console.WriteLine("方式 2 : 原总数:{0} 产生不同数字的数量:{1}", list.Count, r.Count());
        }
    }

    public class TestItem {
        public int Id { get; set; }
        public long Ticks { get; set; }
    }
}

猜你喜欢

转载自blog.csdn.net/yenange/article/details/81540082