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

@ -32,12 +32,27 @@ gets it.
public class Foo { public class Foo {
// Try to avoid this: // Try to avoid this:
synchronized void foo() { 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: // Prefer this:
void bar() { void bar() {
// code, that doesn't need synchronization
// ...
synchronized(this) { synchronized(this) {
if (!sharedData.has("bar")) {
sharedData.add("bar");
} }
} }
// more code, that doesn't need synchronization
// ...
}
// Try to avoid this for static methods: // Try to avoid this for static methods:
static synchronized void fooStatic() { static synchronized void fooStatic() {
@ -45,8 +60,13 @@ public class Foo {
// Prefer this: // Prefer this:
static void barStatic() { static void barStatic() {
// code, that doesn't need synchronization
// ...
synchronized(Foo.class) { synchronized(Foo.class) {
// code, that requires synchronization
} }
// more code, that doesn't need synchronization
// ...
} }
} }
]]> ]]>