In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how Java generates ID through handwritten distributed snowflake SnowFlake". The content is detailed, the steps are clear, and the details are handled properly. I hope this "Java how to generate ID through handwritten distributed snowflake SnowFlake" article can help you solve your doubts.
SnowFlake algorithm
The result of id generated by SnowFlake algorithm is an integer of 64bit size, which has the following structure:
It is divided into four paragraphs:
The first paragraph: 1 bit is unused, always fixed to 0.
(because the highest bit in binary is the sign bit, 1 represents a negative number and 0 represents a positive number. The generated id is usually a positive integer, so the highest bit is fixed to 0)
Second paragraph: 41 bits are millisecond time (41 bits can be used for 69 years)
The third paragraph: 10-bit workerId (10-bit length supports deployment of up to 1024 nodes)
(the 10 bits here are divided into two parts, the first part 5 bits represent the data center ID (0-31), the second part 5 bits represent the machine ID (0-31))
Paragraph 4: 12-bit count within milliseconds (12-bit counting sequence numbers support 4096 ID sequence numbers per node per millisecond)
Code implementation:
Import java.util.HashSet;import java.util.concurrent.atomic.AtomicLong;public class SnowFlake {/ / time 41-bit private static long lastTime = System.currentTimeMillis (); / / data center ID 5-bit (default 0-31) private long datacenterId = 0; private long datacenterIdShift = 5; / / computer room machine ID 5-bit (default 0-31) private long workerId = 0; private long workerIdShift = 5 / / 12-digit random number (default 0,4095) private AtomicLong random = new AtomicLong (); private long randomShift = 12; / / maximum random number private long maxRandom = (long) Math.pow (2, randomShift); public SnowFlake () {} public SnowFlake (long workerIdShift, long datacenterIdShift) {if (workerIdShift)
< 0 || datacenterIdShift < 0 || workerIdShift + datacenterIdShift >22) {throw new IllegalArgumentException ("parameter mismatch");} this.workerIdShift = workerIdShift; this.datacenterIdShift = datacenterIdShift; this.randomShift = 22-datacenterIdShift-workerIdShift; this.maxRandom = (long) Math.pow (2, randomShift);} / / get ID private long getId () {return lastTime of snowflake
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: 0
*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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.