Merge branch 'pr-2695' into master

[java] Improve example for AvoidSynchronizedAtMethodLevel #2695
This commit is contained in:
Andreas Dangel
2020-08-21 10:57:22 +02:00

View File

@ -24,7 +24,7 @@ gets it.
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>//MethodDeclaration[@Synchronized= true()]</value>
<value>//MethodDeclaration[@Synchronized = true()]</value>
</property>
</properties>
<example>
@ -32,12 +32,27 @@ gets it.
public class Foo {
// Try to avoid this:
synchronized void foo() {
// code, that doesn't need synchronization
// ...
// code, that requires synchronization
if (!sharedData.has("bar")) {
sharedData.add("bar");
}
// more code, that doesn't need synchronization
// ...
}
// Prefer this:
void bar() {
// code, that doesn't need synchronization
// ...
synchronized(this) {
if (!sharedData.has("bar")) {
sharedData.add("bar");
}
}
// more code, that doesn't need synchronization
// ...
}
// Try to avoid this for static methods:
static synchronized void fooStatic() {
@ -45,8 +60,13 @@ public class Foo {
// Prefer this:
static void barStatic() {
// code, that doesn't need synchronization
// ...
synchronized(Foo.class) {
// code, that requires synchronization
}
// more code, that doesn't need synchronization
// ...
}
}
]]>