Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze the global ID and snowflake algorithms

Shulou Source: shulou.com Published: 2022-06-01 08:34:30 09月28日 Update

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.

Tags: Content testing overall situation algorithm analysis code situation article more knowledge article good effective task example comrade foundation foreign times notes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei macOS vpn MySQL