Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the global ID and snowflake algorithms

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to analyze the global ID and snowflake algorithms. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

At first I used this simplified version, but later I found that there were duplicates. (demo: https://github.com/dunitian/TempCode/tree/master/2016-11-16/Twitter_Snowflake)

Then rewrite and modify part of the content (https://github.com/ccollie/snowflake-net) on the basis of foreign Daniel, add some notes, etc. [support Core]. Now you can download and use it directly from Nuget: Snowflake.Net

Test case:

Test the code:

Using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Snowflake.Net; namespace Snowflake.ZConsole {class Program {private static int N = 20000000; private static HashSet set = new HashSet (); private static IdWorker worker = new IdWorker (1,1); private static int taskCount = 0; static void Main (string [] args) {Task.Run (() = > GetID ()); Task.Run (() = > Printf ()) Console.ReadKey ();} private static void Printf () {while (taskCount! = 3) {Console.WriteLine ("..."); Thread.Sleep (1000);} Console.WriteLine (set .Count = = N * taskCount);} private static object o = new object (); private static void GetID () {for (var I = 0; I < N; iTunes +) {var id = worker.NextId () Lock (o) {if (set .contains (id)) {Console.WriteLine ("duplicates found: {0}", id);} else {set .add (id);} Console.WriteLine ($"Task {+ + taskCount} complete");}

Maybe some people only care about how to use it in the future. = "

IdWorker worker = new IdWorker (1,1); / / in the case of large concurrency, reducing the number of new can effectively avoid the possibility of repetition

Var id = worker.NextId ()

It is possible that some comrades do not quite understand the above reduction of new, (⊙ o ⊙). For example:

With the test code unchanged, change this sentence:

Complete call to demo: (https://github.com/dunitian/snowflake-net/tree/master/Demo)

Core: (https://github.com/dunitian/snowflake-net/tree/master/Demo.Core)

On how to carry out on the global ID, snowflake algorithm analysis is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 303

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report