From 8488bf843052e51e8f7625ff332a6b4fc5f29c15 Mon Sep 17 00:00:00 2001 From: h0ss <1095067256@qq.com> Date: Mon, 19 Sep 2022 12:39:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9A=8F=E6=9C=BA=E6=95=B0?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=AE=9E=E7=8E=B0=E4=B8=BAThreadLocalRandom?= =?UTF-8?q?=EF=BC=8C=E5=87=8F=E5=B0=91=E5=AF=B9=E8=B1=A1=E5=8F=8D=E5=A4=8D?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=9A=84=E5=BC=80=E9=94=80=EF=BC=8C=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E5=87=8F=E5=B0=91=E7=BA=BF=E7=A8=8B=E4=BA=89=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/dev33/satoken/util/SaFoxUtil.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java index ae2bf386..8665e2f7 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java @@ -10,7 +10,7 @@ import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; -import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Pattern; import cn.dev33.satoken.exception.SaTokenException; @@ -22,6 +22,10 @@ import cn.dev33.satoken.exception.SaTokenException; * */ public class SaFoxUtil { + /** + * 可以减少线程争用的随机对象 + */ + private static final ThreadLocalRandom random = ThreadLocalRandom.current(); private SaFoxUtil() { } @@ -52,7 +56,6 @@ public class SaFoxUtil { */ public static String getRandomString(int length) { String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(62); @@ -95,7 +98,7 @@ public class SaFoxUtil { * @return 随机字符串 */ public static String getMarking28() { - return System.currentTimeMillis() + "" + new Random().nextInt(Integer.MAX_VALUE); + return System.currentTimeMillis() + "" + random.nextInt(Integer.MAX_VALUE); } /**