正则表达式 cpu100%
这一段时间发现,在项目实时查询交易中总是出现cpu100%的情况,但是程序中并未出现死循环的情况,最后通过打印日志发现,问题出现在转大小写的方法中:
public static String lowerCaseJsonKey(String jsonInput){
int i=0;
String originalInput = jsonInput;
StringBuilder inputStr = new StringBuilder(jsonInput);
String regex = "\"(\\w+)\":";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(inputStr);
while(m.find()){
String valueName = m.group(1);
String regx1 = "\""+valueName+"\":";
String replace = "\""+valueName.toLowerCase()+"\":";
originalInput=originalInput.replaceAll(regx1, replace);
inputStr.delete(0, m.end(0));
m=p.matcher(inputStr);
i++;
//
}
System.out.println("i:"+i);
return originalInput;
}
问题出现在正则表达式上,正则表达式在数据量较小的时候不会出现问题,但是在大数据量或者并发很高的时候,cpu在短时间内load值会非常的高,所以不建议在此情况下使用。
转载于:https://blog.51cto.com/alentain/972256
更多推荐
所有评论(0)