当前位置:网站首页>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,所以会抛出异常.
边栏推荐
- rider内Mono脚本找不到引用资源
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- 从源码角度分析UUID的实现原理
- LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
- LeetCode_628_三个数的最大乘积
- Open Office XML 格式里如何描述多段具有不同字体设置的段落
- 微信小程序提交审核历史版本记录从哪里查看
- Weilai-software development engineer side record
- 老板加薪!看我做的WPF Loading!!!
- Introduction to Software Architecture
猜你喜欢
单目操作符(含原码反码补码转换)
做自媒体月入几万?博主们都在用的几个自媒体工具
From the product dimension, why can't we fully trust Layer2?
从脚本到剪辑,影像大师亲授的后期制作秘籍
基于UiAutomator2+PageObject模式开展APP自动化测试实战
蔚来-软件开发工程师一面记录
中小规模网站架构
The impact of development mode on testing
The brave rice rice, does not fear the brush list of 】 list has a ring
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
随机推荐
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
快速上手,征服三种不同分布式架构调用方案
POJ 3101 Astronomy (数学)
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
阻塞 非阻塞 poll机制 异步
基于UiAutomator2+PageObject模式开展APP自动化测试实战
ViT结构详解(附pytorch代码)
Licking Exercise - 63 Find all anagrams in a string
Centos7环境使用Mysql离线安装包安装Mysql5.7
力扣练习——58 验证二叉搜索树
什么是幂等性?四种接口幂等性方案详解!
The brave rice rice, does not fear the brush list of 】 list has a ring
使用.NET简单实现一个Redis的高性能克隆版(六)
std::move()
POJ 3101 Astronomy (Mathematics)
HDU 6040 Hints of sd0061 (技巧)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
[Go WebSocket] 多房间的聊天室(一)思考篇
Will SQL and NoSQL eventually converge?