线程安全的ID生成器
介绍一个改造升级版本的雪花算法ID生成器,
优点是线程安全的,并且永远不可能出现重复ID。放心使用。
目前网上流行的ID生成器,在for循环里,或者多线程的时候会出现重复。
介绍一个新出炉的开源ID生成器,完美解决这个问题。
示例源码地址:https://github.com/geektcp/example-id-generator
核心是thy-core依赖包里面的IdGenerator类的getId方法,不传参数就直接使用,也可以传参数进一步细分。
另外在spring的entity类的主键上加入注解,自动生成。
在for循环或者多线程环境下任意使用都不会有问题。
package com.geektcp.id; import com.geektcp.common.core.generator.IdGenerator; public class App { public static void main(String[] args) { base(); singleGen(); multiThread(); } private static void base() { long a = 0; String c = null; a = IdGenerator.getId(); System.out.println(a); a = IdGenerator.getId(1, 3); System.out.println(a); IdGenerator.setSplit("/"); c = IdGenerator.getId("PRE"); System.out.println(c); IdGenerator.setInstance(10, 0); a = IdGenerator.getId(); System.out.println(a); } private static void singleGen() { for (int i = 0; i < 100; i++) { System.out.println(IdGenerator.getId()); } } private static void multiThread() { new Thread(() -> { for (int i = 0; i < 100; i++) { System.out.println(IdGenerator.getId("A")); } }, "A").start(); new Thread(() -> { for (int i = 0; i < 100; i++) { System.out.println(IdGenerator.getId("B")); } }, "A").start(); } }
java依赖包,直接在maven官方仓库搜索(https://mvnrepository.com/artifact/com.geektcp.common)
thy-core
这里采用
1.0.0.RELEASE
最新版本即可。
<dependencies> <!-- https://mvnrepository.com/artifact/com.geektcp.common/thy-core --> <dependency> <groupId>com.geektcp.common</groupId> <artifactId>thy-core</artifactId> <version>1.0.0.RELEASE</version> </dependency> </dependencies>
打印结果:
1069366794495500288 1069366794499694592 PRE/1069366794503888896 1069366794503888897 1069366794503888898 1069366794503888899 1069366794503888900 1069366794503888901 1069366794503888902 1069366794503888913 1069366794503888914 1069366794503888915 1069366794520666134 1069366794520666135 1069366794520666136 1069366794520666137 1069366794520666138 1069366794520666139 1069366794520666140 1069366794520666141 1069366794520666142 1069366794520666143 1069366794520666144 A/1069366795044954112 A/1069366795044954113 A/1069366795053342723 A/1069366795053342725 A/1069366795053342726 A/1069366795053342727 A/1069366795053342728 A/1069366795057537024 A/1069366795057537025 A/1069366795057537034 A/1069366795057537035 A/1069366795057537036 A/1069366795057537037 A/1069366795057537038 A/1069366795057537039 A/1069366795057537040 B/1069366795053342724 B/1069366795070119936 B/1069366795070119937 B/1069366795070119938 B/1069366795070119939 B/1069366795070119940 B/1069366795070119941 B/1069366795070119942 B/1069366795070119943 B/1069366795070119944 B/1069366795070119945 B/1069366795070119946 B/1069366795070119947 B/1069366795070119965 B/1069366795070119966 B/1069366795070119967 B/1069366795070119968 B/1069366795070119969 B/1069366795074314240 B/1069366795074314241 B/1069366795074314242 B/1069366795074314243 B/1069366795074314244 B/1069366795078508559 B/1069366795078508560 B/1069366795082702851 A/1069366795095285798 A/1069366795095285799 A/1069366795095285800 A/1069366795095285801 A/1069366795116257280 A/1069366795116257281 A/1069366795116257282 A/1069366795116257283 A/1069366795116257284 A/1069366795116257285
-------------------------
技术不在于多么高超先进巧妙,而在于要有现实价值!!!
技术不在于多么高超先进巧妙,而在于要有现实价值!!!