简体   繁体   English

避免在方法级别同步

[英]Avoid synchronized at method level

PMD (source code analyzer) recommends to avoid synchronized at method level. PMD(源代码分析器)建议避免在方法级别synchronized It is actually obvious as when you call synchronized on method you are calling synchronized on this and that can lead to potential issues.实际上很明显,当您在方法上调用synchronized时,您正在为此调用同步, this可能会导致潜在的问题。

But PMD in this rule suggests the following example:Synchronized on method rule但此规则中的 PMD 建议使用以下示例:Synchronized on method rule

public class Foo {
  // Try to avoid this:
  synchronized void foo() {
  }
  // Prefer this: <----- Why? Isn't it the same on byte-code level
  void bar() {
    synchronized(this) {
    }
  }
}

Is it a bug in a specification of PMD (in the example) or synchronized on method works differently from synchronized(this)?它是 PMD 规范中的错误(在示例中)还是同步方法与同步(this)的工作方式不同?

Yes, it is technically the same.是的,它在技术上是相同的。 This is about code style, not correctness.这是关于代码风格,而不是正确性。

The link you posted explains the reason quite well:您发布的链接很好地解释了原因:

Method-level synchronization can cause problems when new code is added to the method.将新代码添加到方法时,方法级同步可能会导致问题。 Block-level synchronization helps to ensure that only the code that needs synchronization gets it.块级同步有助于确保只有需要同步的代码才能获得它。

An additional rule in SB_CONTRIB discourages use of this in synchronized since the class object can be used by other threads to lock on as well. SB_CONTRIB 中的一个附加规则不鼓励在同步中使用它,因为 class object 也可以被其他线程用来锁定。

http://fb-contrib.sourceforge.net/bugdescriptions.html http://fb-contrib.sourceforge.net/bugdescriptions.html

NOS_NON_OWNED_SYNCHRONIZATION This method uses a synchronize block where the object that is being synchronized on, is not owned by this current instance. NOS_NON_OWNED_SYNCHRONIZATION 此方法使用同步块,其中正在同步的 object 不归当前实例所有。 This means that other instances may use this same object for synchronization for their own purposes, causing synchronization confusion.这意味着其他实例可能会出于自己的目的使用相同的 object 进行同步,从而导致同步混乱。 It is always cleaner and safer to only synchronize on private fields of this class.仅在此 class 的私有字段上同步总是更清洁、更安全。 Note that 'this' is not owned by the current instance, but is owned by whomever assigns it to a field of its class.请注意,“this”不属于当前实例,而是由将其分配给其 class 字段的任何人所有。 Synchronizing on 'this' is also not a good idea.在“this”上同步也不是一个好主意。

In conjunction with this Spotbugs rule, it is more than good style to not synchronize the entire method body.结合这个 Spotbugs 规则,不同步整个方法体是一种很好的风格。

The “correct” versions are bugs in the PMD documentation, at the very least. “正确”的版本至少是 PMD 文档中的错误。

The point of the rule is to indicate that placing an entire method body in a synchronization block is probably unnecessary;该规则的要点是表明将整个方法体放在同步块中可能是不必要的; in most cases, only the code accessing a multi-threaded resource needs to be synchronized.大多数情况下,只有访问多线程资源的代码需要同步。 So the examples should show, or imply, that there is some code before and/or after the synchronized block.因此,这些示例应该显示或暗示在同步块之前和/或之后有一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM