FindBugs问题集锦

问题一:字符串相关

文字描述

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关键字,并且使用双层检测锁既可解决

图片描述:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值