Identity自增序列/唯一断标识

ThreadStatic应用(Identity补完)

 

用于在高并发环境中的自增序列维护和快速创建唯一不重复的短标识,该类是线程安全的

如在ORM组件中,创建唯一的参数名

特点:

高并发环境下的性能保证

创建的唯一标识较短且在一定条件下绝不重复

在特定环境中实用价值较高

缺点:

有上限限制,短时间内并发超过上限则会出现重复,理论上限值ulong.MaxValue,不过建议不要设置过高

 

/// <summary> 自增序列,最大0xFFFFFFF,超过0xFFFFFFF回归1
/// </summary>
public static class Identity
{
    static volatile int _id = 0;
    /// <summary> 当前值
    /// </summary>
    public static int Current
    {
        get { return Identity._id; }
    }
    /// <summary> 获取一个id,每获取一次就会自增1,该方法在所有线程都是安全的
    /// </summary>
    public static int Next()
    {
        int i = System.Threading.Interlocked.Increment(ref _id);
        if (i > (0xFFFFFFF))
        {
            i = i - 0xFFFFFFF;
            System.Threading.Interlocked.Exchange(ref _id, i);
        }
        return i;
    }

    //字符字典
    static char[] _CharMap = new[]
    {
        'a','b','c','d','e','f','g','h','i','j','k','l','m',
        'n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M',
        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    };


    /// <summary> 获取一个id的String表示形式,每获取一次就会自增1,该方法在所有线程都是安全的
    /// </summary>
    public static string NextString()
    {
        int number = Next();
        int length = (int)Math.Log(number, 52) + 1;//52比较合理,不要改了
        char[] c = new char[length + 1];
        c[length] = '0';
        for (int i = length - 1; i > 0; i--)
        {
            c[i] = _CharMap[number % 52];
            number = number / 52;
        }
        c[0] = _CharMap[number];
        return new string(c);
    }
}

 

代码中的初始值是从0开始的. 为了方便看效果,我修改初始值,为0xFFFFFFC

            Console.Write("初始值:");
            Console.WriteLine(Identity.Current);
            Console.WriteLine("==== 数字 ====");
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());
            Console.WriteLine(Identity.Next());

 

初始值:268435452
==== 数字 ====
268435453
268435454
268435455
1
2
3
4

 

 这里也修改了初始值

            Console.Write("初始值:");
            Console.WriteLine(Identity.Current);
            Console.WriteLine("==== 字符 ====");
            for (int i = 0; i < 20; i++)
            {
                Console.Write(Identity.NextString());
                Console.Write("\t当前值:");
                Console.WriteLine(Identity.Current);
            }

 

初始值:268423164
==== 字符 ====
KLaVX0 当前值:268423165
KLaVY0 当前值:268423166
KLaVZ0 当前值:268423167
KLaWa0 当前值:268423168
KLaWb0 当前值:268423169
KLaWc0 当前值:268423170
KLaWd0 当前值:268423171
KLaWe0 当前值:268423172
KLaWf0 当前值:268423173
KLaWg0 当前值:268423174
KLaWh0 当前值:268423175
KLaWi0 当前值:268423176
KLaWj0 当前值:268423177
KLaWk0 当前值:268423178
KLaWl0 当前值:268423179
KLaWm0 当前值:268423180
KLaWn0 当前值:268423181
KLaWo0 当前值:268423182
KLaWp0 当前值:268423183
KLaWq0 当前值:268423184

 https://code.csdn.net/snippets/104950

posted @ 2013-12-09 21:58  冰麟轻武  阅读(1284)  评论(2编辑  收藏  举报