From 33e1c1abaf34ffa24135ab6f861961188e932962 Mon Sep 17 00:00:00 2001 From: jiajia Date: Tue, 21 May 2024 09:34:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=AA=E8=8A=B1=E7=AE=97=E6=B3=95=E5=8D=95?= =?UTF-8?q?=E5=88=97=E6=A8=A1=E5=BC=8F=E5=AE=9E=E4=BE=8B=E5=8C=96=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pojo/base/codemaker/SnowflakeIdMaker.java | 30 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/codemaker/SnowflakeIdMaker.java b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/codemaker/SnowflakeIdMaker.java index 7441b96..6bb684e 100644 --- a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/codemaker/SnowflakeIdMaker.java +++ b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/codemaker/SnowflakeIdMaker.java @@ -32,6 +32,26 @@ public class SnowflakeIdMaker { private long datacenterId; private long sequence = 0L; + private volatile static SnowflakeIdMaker snowflakeIdMaker = null; + + + + + /** + * 单列模式实例化对象 + * @return + */ + public static SnowflakeIdMaker getSingleton(){ + if (snowflakeIdMaker == null){ + synchronized (SnowflakeIdMaker.class){ + if (snowflakeIdMaker == null) { + snowflakeIdMaker = new SnowflakeIdMaker(); + } + } + } + return snowflakeIdMaker; + } + /** * 禁止私自构建,防止ID重复 */ @@ -42,6 +62,8 @@ public class SnowflakeIdMaker { this.sequence = 0L; } + + /** * 禁止私自构建 防止 ID 生成重复 * @param workerId 工作区ID @@ -104,12 +126,12 @@ public class SnowflakeIdMaker { private long lastTimestamp = -1L; - private long getWorkerId(){ - return workerId; + public long getWorkerId(){ + return this.workerId; } - private long getDatacenterId(){ - return datacenterId; + public long getDatacenterId(){ + return this.datacenterId; } public synchronized long nextId() {