当前位置:网站首页>Analysis of the implementation principle of UUID from the perspective of source code
Analysis of the implementation principle of UUID from the perspective of source code
2022-08-10 11:52:00 【51CTO】
这是 《水煮 JDK 源码》系列 的第 11 篇文章,计划撰写100篇关于JDK源码相关的文章
UUID
的全称是 universally unique identifier
,表示通用唯一标识符,UUID
类位于 java.util
包下,自 JDK 1.5
版本新增的,它是一个 final
类,不能被继承,在平常的开发中,通常会使用 UUID
Class to generate a unique identifier,比如下面的代码:
运行后,输出的结果如下:
The results above show is the default UUID
字符串,它是由数字、字母和 - 组成,The numbers and letters together32个字符,- 占4个字符,总共36个字符,当我们运行上面的程序时,实际上会调用 UUID
哪些方法呢?具体如下:
那么 UUID
是如何生成的呢?32What does each character mean??Let's take a look at the specific source code below.
1、UUID 类定义
UUID
类实现了 Serializable
和 Comparable
接口,其定义如下:
Serializable
:它是一个标记接口,no method definition,用于对象的序列化;Comparable
:该接口只有一个compareTo
方法,Usually used to compare the size of objects of the class that implements it;
2、成员变量
UUID
类中定义了2个成员变量,respectively represent the most effective64bit and least significant64位,如下:
mostSigBits
和 leastSigBits
它们是构成 UUID
An important part of the identifier.
3、构造函数
UUID
类提供了 2 个构造函数,其定义如下:
在构造函数中,Mainly for member variables mostSigBits
和 leastSigBits
赋值,虽然 UUID
提供了 public
构造函数,但是在平时开发中,may rarely be created directly through the constructor UUID
对象,更多的是使用 randomUUID()
方法,Breakpoint way below look at some of the calculated the private constructor mostSigBits
和 leastSigBits
具体值是多少,如下:
4、方法
UUID
The methods in a class are mainly divided into static methods and instance methods,Among them, static methods are mainly used to create UUID
实例的,Instance methods are mainly used to obtain UUID
中的一些基本信息,比如版本号、时间戳、clock sequence etc..
4.1 静态方法 - randomUUID()
randomUUID()
The method is probably the most used one way,从实现可以看出,First it through internal static class Holder
got a random SecureRandom
对象,主要用于产生随机数,Holder
类定义如下:
4.2 静态方法 - nameUUIDFromBytes()
nameUUIDFromBytes()
methods can be created from a byte array UUID
,首先会通过 MD5
Digest algorithm cryptographically transforms byte arrays,得到一个长度为 16 的新字节数组,Then build with the new byte array UUID
,This method will set the version number to 3,而通过 randomUUID()
The version number set by the method is 4.
4.3 静态方法 - fromString()
In addition to the above which can be constructed from byte arrays UUID
外,You can also build directly from strings,But not just any string,but requires and UUID.toString()
The string format obtained by the method is consistent,也就是使用 -
For at the time of the partitioning,must be of length 5 的字符串数组,否则就会抛出 IllegalArgumentException
异常,Then according to the divided value to calculate mostSigBits
和 leastSigBits
的值,最后再通过 mostSigBits
和 leastSigBits
构建 UUID
对象.
4.4 实例方法
UUID
类提供的实例方法,主要有以下这些:
version()
: 获取当前UUID
的版本信息;variant()
:获取当前UUID
variant number of;timestamp()
:获取当前UUID
的时间戳;clockSequence()
:获取当前UUID
The clock sequence value of;node()
:获取当前UUID
的节点值;toString()
:将UUID
对象转换为字符串,In this way, you can understandUUID
的构成;hashCode()
:获取当前UUID
的哈希值;equals(Object obj)
:用于比较两个UUID
对象是否相同;compareTo(UUID val)
:比较两个UUID
the size of the object value;
(1)Basic Information Method
For version number and variant number information,是所有 UUID
通用的,For time-based UUID
,then you can call the following 3 methods to obtain time-related information,如下:
(2)toString() 方法
通过 toString()
方法,可以得知 UUID
The specific composition of the string,主要是通过 mostSigBits
和 leastSigBits
obtained by transforming,digits()
方法定义如下:
UUID
The composition is mainly divided into the following:
通过上面的信息可以看出,UUID
由 时间低位
+ time median
+ Time high and version number
+ Variant number and serial number
+ 节点值
五个部分组成的,can see a specific value
(3)其他方法
For comparing two UUID
Is it the same or is it equal,are direct comparisons mostSigBits
和 leastSigBits
的值.
5、测试验证
下面通过 3 种不同的方式来创建 UUID
实例,Simultaneously output basic information.
运行程序,输出信息如下:
If directly to the creation of the above UUID
Invoke time-dependent methods,则会抛出 UnsupportedOperationException
异常,测试代码如下:
The output error message is as follows:
It can be known from the previous source code analysis,timestamp()
、clockSequence()
、node()
Methods must be time-based UUID
才能调用,基于时间的 UUID
的版本号为 1
,而上面的 uuid
、uuid1
、uuid2
Neither is time-based,所以会抛出异常.
边栏推荐
猜你喜欢
Flutter气泡框实现
如何使用工程仪器设备在线监测管理系统
Spss-多元回归案例实操
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
推荐6个自媒体领域,轻松易上手
为什么Redis很快
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
Where can I view the version record of WeChat applet submission review history?
从产品维度来看 我们为什么不能完全信任Layer2?
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
随机推荐
网络套接字(UDP和TCP编程)
老板加薪!看我做的WPF Loading!!!
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
学长告诉我,大厂MySQL都是通过SSH连接的
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
POJ 1026 Cipher (Permutation Groups)
Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
态路小课堂丨如何为CXP光模块选择光纤跳线?
石墨文档打开文档时快速定位到上次写的位置
从源码角度分析UUID的实现原理
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
从产品角度看 L2 应用:为什么说这是一个游乐场?
[E-commerce operation] Do you really understand social media marketing (SMM)?
使用.NET简单实现一个Redis的高性能克隆版(六)
越折腾越好用的 3 款开源 APP
力扣练习——56 寻找右区间
【LeetCode】640. 求解方程
推荐6个自媒体领域,轻松易上手