当前位置:网站首页>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,所以会抛出异常.
边栏推荐
- 怎么加入自媒体,了解这5种变现模式,让账号快速变现
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- 力扣练习——62 有效的数独
- Buckle Exercise - 61 Sort by frequency of characters
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- Licking Exercise - 63 Find all anagrams in a string
- Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
- 常量及数据类型你还记得多少?
- HDU 4372:Count the Buildings (Stirling数)
猜你喜欢
随机推荐
怎么加入自媒体,了解这5种变现模式,让账号快速变现
Licking Exercise - 60 Maximum key-value sum of binary search subtrees
Licking Exercise - 58 Verifying Binary Search Trees
机器学习之暴力调参案例
SQL优化最强总结 (建议收藏~)
POJ 2891 Strange Way to Express Integers (扩展欧几里得)
【勇敢饭饭,不怕刷题之链表】有序链表的合并
APP automation testing practice based on UiAutomator2+PageObject mode
从产品角度看 L2 应用:为什么说这是一个游乐场?
自媒体爆款标题怎么写?手把手教你写热门标题
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
推荐6个自媒体领域,轻松易上手
快速上手,征服三种不同分布式架构调用方案
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
不止跑路,拯救误操作rm -rf /*的小伙儿
HDU 1520 Anniversary party (tree dp)
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
常量及数据类型你还记得多少?
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
Nocalhost - 让云原生时代的开发更高效