Synchronizing at the method, rather than the block level could lead to problems when maintenance adds code to the method, perhaps inadvertently synchronizing it as well. Instead, synchronization should be applied to the smallest feasible block for optimum performance and maintainability.


== Noncompliant Code Example

----
public class MyClass() {

  public void synchronized doSomething() { // Noncompliant
    // ...
  }
}
----


== Compliant Solution

----
public class MyClass() {

  private Object lockObj = new Object();

  public void doSomething() {
    synchronized(lockObj) {
      // ...
    }
  }
}
----


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
