问题一:字符串相关
文字描述:
The method seems to be building a String using concatenation in a loop. In each iteration, the String is converted to a StringBuffer/StringBuilder, appended to, and converted back to a String. This can lead to a cost quadratic in the number of iterations, as the growing string is recopied in each iteration.
该方法似乎是在循环中使用连接构建String。在每次迭代中,String被转换为StringBuffer/StringBuilder,然后追加到,再转换回String。这可能导致迭代次数的成本为二次元,因为在每次迭代中都会重新复制不断增长的字符串。
图片描述:
解决方案:
通过显式使用StringBuffer(或Java 1.5中的StringBuilder)可以获得更好的性能。
图片描述:
问题二:Map相关
文字描述:
This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.
此方法使用从keySet迭代器检索的键来访问Map条目的值。在映射的entrySet上使用迭代器更有效,以避免map .get(key)查找。
图片描述:
解决方案:
建议你将Map对象中的键值对集合遍历时,使用Map.entrySet()方法返回的键值对集合进行迭代查找键值。因为在使用Map.entrySet()方法返回的键值对集合时,Map对象只需要计算一次哈希值就可以同时获取到键和值,这样可以避免在遍历时每次都需要重新计算键的哈希值的性能消耗。
图片描述:
问题三:线程相关
文字描述:
This method contains an unsynchronized lazy initialization of a non-volatile static field. Because the compiler or processor may reorder instructions, threads are not guaranteed to see a completely initialized object, if the method can be called by multiple threads. You can make the field volatile to correct the problem. For more information, see the Java Memory Model web site.
此方法包含非易失性静态字段的非同步延迟初始化。因为编译器或处理器可能会重新排序指令,如果方法可以被多个线程调用,线程不能保证看到一个完全初始化的对象。您可以将字段设置为易失性来纠正这个问题。有关更多信息,请参阅Java内存模型网站。
图片描述:
解决方案:
在字段前面加上volatile关键字,并且使用双层检测锁既可解决
图片描述: