修改随机数获取实现为ThreadLocalRandom,减少对象反复创建的开销,同时减少线程争用

This commit is contained in:
h0ss
2022-09-19 12:41:21 +08:00
parent 8488bf8430
commit 5342bd5586

View File

@@ -25,7 +25,7 @@ public class SaFoxUtil {
/**
* 可以减少线程争用的随机对象
*/
private static final ThreadLocalRandom random = ThreadLocalRandom.current();
private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
private SaFoxUtil() {
}
@@ -58,7 +58,7 @@ public class SaFoxUtil {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(62);
int number = RANDOM.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
@@ -98,7 +98,7 @@ public class SaFoxUtil {
* @return 随机字符串
*/
public static String getMarking28() {
return System.currentTimeMillis() + "" + random.nextInt(Integer.MAX_VALUE);
return System.currentTimeMillis() + "" + RANDOM.nextInt(Integer.MAX_VALUE);
}
/**