166 Star 1.8K Fork 461

yitter / 多语言新雪花算法(SnowFlake IdGenerator)

 / 详情

大并发下是否存在重复问题

已完成
创建于  
2022-11-18 17:54

var op = new IdGeneratorOptions (1) { WorkerIdBitLength = 6 };
YitIdHelper.SetIdGenerator (op);

        Dictionary<long, long> dict = new Dictionary<long, long> ();
        List<Task> list = new List<Task>();
        for(int i = 0; i < 2000; i++)
        {
            var task = Task.Run (() =>
            {
                var newId = YitIdHelper.NextId ();
                dict.Add (newId, i);
            });
            list.Add (task);    
        }

        list.ForEach (item =>
        {
            System.Threading.Thread.Sleep (1);
            item.Wait ();
        });
        int iw = 0;      

这样子测试的话 会有生成不了的情况
预期 2000个
实际 小于2000
应该是生成重复或则失败了

评论 (2)

微帮帮 创建了任务

是你用的这个 Dictionary 的并发有问题。你直接 Insert 到 DataBase 看看。

还真是
换成了 ConcurrentDictionary<long, long> dict = new ConcurrentDictionary<long, long> (); 测试了2000 没有问题 ;棒棒的 :+1:

yitter 任务状态待办的 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
Java
1
https://gitee.com/yitter/idgenerator.git
git@gitee.com:yitter/idgenerator.git
yitter
idgenerator
多语言新雪花算法(SnowFlake IdGenerator)

搜索帮助

344bd9b3 5694891 D2dac590 5694891