我使用一个字符串作为锁,所以想确保该对象是一个新实例。 FindBugs 提示是因为直接定义字符串(使用双引号)通常更有效。我的代码如下所示:
/** A lock for the list of inputs. */
@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_STRING_CTOR")
//We want a new String object here as this is a lock.
private final Object inputListLock = new String("inputListLock");
我是不是做错了什么? Eclipse FindBugs 插件仍然将此报告为问题:
Pattern id: DM_STRING_CTOR, type: Dm, category: PERFORMANCE Using the java.lang.String(String) constructor wastes memory because the object so constructed will be functionally indistinguishable from the String passed as a parameter. Just use the argument String directly.
最佳答案
为什么不直接将锁对象声明为一个新对象呢?您不需要将它变成一个字符串,因为您没有做任何需要锁的字符串特性的事情,并且可以推测您除了锁定之外不会将其用于任何其他用途。
在没有看到您的其余代码的情况下,我可以冒险猜测您正在锁定对某种列表的访问。您可以使用列表本身作为锁定对象。如果它是私有(private)的,那么其他人就不可能造成死锁。
关于java - 在 Eclipse 中抑制 FindBugs 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976328/