-
启动Arthas
java -jar arthas-boot.jar
-
执行dashboard
dashboard
线程 35 和 12042 不正常 CUP 占用 49%
-
定位代码行
thread 35 thread 12042
-
查看代码
需求为生成一个至少包含 2 个数字的随机字符串,使用的统一的工具类方法,该方法中先通过 UUID.randomUUID() 随机出一个 10 位的字符池,然后再从这个字符池中随机需要位数的字符串,如果随机出来的 10 位字符池中都是字母,则二次随机时候就会出现死循环
问题代码如下:
public static String getRandomStr(boolean numberFlag, int length) { String retStr = ""; String strTable = numberFlag ? UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10) : "1234567890abcdefghijkmnpqrstuvwxyz"; int len = strTable.length(); boolean bDone = true; do { retStr = ""; int count = 0; for (int i = 0; i < length; i++) { double dblR = Math.random() * len; int intR = (int) Math.floor(dblR); char c = strTable.charAt(intR); if (('0' <= c) && (c <= '9')) { count++; } retStr += strTable.charAt(intR); } if (count >= 2) { bDone = false; } } while (bDone); return retStr; }
最终原因是死循环导致的 CPU 飚高,修复代码,增加是否都是字母的判断,第一次随机出来的 10 位字符池都是字母,则重新随机。
Arthas排查生产环境CPU飚高问题
最新推荐文章于 2024-06-03 16:43:31 发布