rspec/rules/S3013/java/rule.adoc

31 lines
620 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
public class MyClass() {
public void synchronized doSomething() { // Noncompliant
// ...
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
public class MyClass() {
private Object lockObj = new Object();
public void doSomething() {
synchronized(lockObj) {
// ...
}
}
}
----