From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java | 55 ++++++++++++++++++++++++++++++++++---------------------
1 files changed, 34 insertions(+), 21 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java
index 7c607d9..3ab0397 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/IDLongGenerator.java
@@ -1,13 +1,17 @@
package com.dy.common.util;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Calendar;
+@Slf4j
@Component
public class IDLongGenerator {
- private static final int yearLenght = 4 ;//4锛氬勾搴﹀彇鍥涗綅锛� 3锛氬勾搴﹀彇涓変綅锛� 2锛氬勾搴﹀彇涓や綅锛� 1锛氬勾搴﹀彇涓�浣嶏紝 0锛氬勾搴﹀彇0浣�
+ private static final Object synObj = new Object() ;
+
+ private static final int yearLength = 4 ;//4锛氬勾搴﹀彇鍥涗綅锛� 3锛氬勾搴﹀彇涓変綅锛� 2锛氬勾搴﹀彇涓や綅锛� 1锛氬勾搴﹀彇涓�浣嶏紝 0锛氬勾搴﹀彇0浣�
private static int add = 0 ;
private static final int chengShu = 1000 ;//1000锛氫笁浣嶈嚜澧為噺锛屽嵆涓�绉掗挓鍙骇鐢�10000涓狪D
@@ -18,17 +22,23 @@
//姝D鐢熸垚鍣ㄥ湪鍚勪釜瀛愮郴缁熶腑闅惧厤涓哄悓涓�绫绘暟鎹敓鎴愮浉鍚岀殑ID锛岄�犳垚鏁版嵁搴撴彃鍏ュ洜涓婚敭鐩稿悓鑰屾姤閿�,
//鎵�浠ヨ璁℃鍚庣紑锛屾瘡涓瓙绯荤粺鍚庣紑涓嶅悓
private static int suffix = 0 ;
+ private static final int noSuffix = 0 ;
static {
last = current() ;
}
/**
- * 23 10 28 09 14 40 00000
- * 闀垮害16鐨処D锛屽勾搴﹀彇涓や綅锛岄暱搴�18鐨処D锛屽勾搴﹀彇鍥涗綅锛�17浣嶆暟瀛楄秴鍑轰簡javascript鐨勮〃鏁拌寖鍥达紝javascript浼氳〃鏁颁笉姝g‘
+ * 2023 10 28 09 14 40 00000
+ * 闀垮害19鐨処D锛屽勾搴﹀彇涓や綅锛岄暱搴�18鐨処D锛屽勾搴﹀彇鍥涗綅锛�17浣嶆暟瀛楄秴鍑轰簡javascript鐨勮〃鏁拌寖鍥达紝javascript浼氳〃鏁颁笉姝g‘
*/
public Long generate(){
- return doGenerate() ;
+ synchronized (synObj){
+ //Long id = doGenerate() ;
+ //log.info("浜х敓ID = " + id);
+ //return id ;
+ return doGenerate() ;
+ }
}
/**
* 璁剧疆鍚庣紑锛屼笉鍚屽瓙绯荤粺璁剧疆涓嶅悓鐨勫悗缂�
@@ -47,7 +57,7 @@
* @return ID
*/
public static Long generateTodayStartId(){
- return (currentTodayStart() * chengShu) * 100 + suffix;
+ return (currentTodayStart() * chengShu) * 100 + noSuffix;
}
/**
@@ -55,7 +65,7 @@
* @return ID
*/
public static Long generateTodayEndId(){
- return (currentTodayEnd() * chengShu) * 100 + suffix;
+ return (currentTodayEnd() * chengShu) * 100 + noSuffix;
}
@@ -64,7 +74,7 @@
* @return ID
*/
public static Long generateBeforeXDayStartId(int xday){
- return (beforeXDayStart(xday) * chengShu) * 100 + suffix;
+ return (beforeXDayStart(xday) * chengShu) * 100 + noSuffix;
}
/**
@@ -72,7 +82,7 @@
* @return ID
*/
public static Long generateBeforeXDayEndId(int xday){
- return (beforeXDayEnd(xday) * chengShu) * 100 + suffix;
+ return (beforeXDayEnd(xday) * chengShu) * 100 + noSuffix;
}
@@ -96,26 +106,26 @@
/**
* 鎵ц鐢熸垚
- * @return ID
+ * @return ID 20231218 104504 06900
*/
- private synchronized Long doGenerate(){
+ private Long doGenerate(){
long id ;
long now = current() ;
if(now != last){
//涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 涓嶅湪鍚屼竴绉掑唴
last = now ;
add = 0 ;
- id = last * chengShu + add ++;
+ id = last * chengShu + add++;
}else{
//涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 鍦ㄥ悓涓�绉掑唴
if(add >= maxAdd){
//闄勫姞閲忓凡缁忕敤灏�
waitNextSecond(last) ;//绛夊埌涓嬩竴绉�
- id = last * chengShu + add ++ ;//杩斿洖涓婁竴绉掔敓鎴愮殑ID
+ id = last * chengShu + add++ ;//杩斿洖涓婁竴绉掔敓鎴愮殑ID
add = 0 ;//闄勫姞閲忓綊闆讹紝涓轰笅涓�绉掑噯澶�
}else{
//闄勫姞閲忔湭鐢ㄥ敖
- id = last * chengShu + add ++ ;
+ id = last * chengShu + add++ ;
}
}
return (id * 100) + suffix ;
@@ -229,28 +239,31 @@
* @return 澶勭悊鍚庣殑骞村害
*/
private static int dealYear(int year){
- if(yearLenght == 0){
+ if(yearLength == 0){
return 0 ;
- }else if(yearLenght == 1){
+ }else if(yearLength == 1){
return year % 10 ;
- }else if(yearLenght == 2){
+ }else if(yearLength == 2){
return year % 100 ;
- }else if(yearLenght == 3){
+ }else if(yearLength == 3){
return year % 1000 ;
- }else if(yearLenght == 4){
+ }else if(yearLength == 4){
return year ;
}else{
return year ;
}
}
-//
+
// public static void main(String[] args){
+// Calendar cal = Calendar.getInstance();
+// System.out.println(cal.get(Calendar.MONTH) + 1) ;
+//
// IDLongGenerator o = new IDLongGenerator() ;
-// int total = 800 ;
-// long start = System.currentTimeMillis() ;
+// int total = 1 ;
// for(int i = 0 ; i < total ; i++){
// System.out.println(o.generate()) ;
// }
+// long start = System.currentTimeMillis() ;
// long end = System.currentTimeMillis() ;
// System.out.println("浜х敓" + total + "ID鐢ㄦ椂" + (end - start) + "姣");
//
--
Gitblit v1.8.0