Why is this an issue?
A synchronized method is a method marked with the synchronized
keyword, meaning it can only be accessed by one thread at a time. If
multiple threads try to access the synchronized method simultaneously, they will be blocked until the method is available.
Synchronized methods prevent race conditions and data inconsistencies in multi-threaded environments. Ensuring that only one thread can access a
method at a time, prevents multiple threads from modifying the same data simultaneously, and causing conflicts.
When one part of a getter/setter pair is synchronized
the other should be too. Failure to synchronize both sides may result in
inconsistent behavior at runtime as callers access an inconsistent method state.
This rule raises an issue when either the method or the contents of one method in a getter/setter pair are synchronized, but the other is not.
How to fix it
Synchronize both get
and set
methods by marking the method with the synchronize
keyword or using a
synchronize
block inside them.
Code examples
Noncompliant code example
public class Person {
String name;
int age;
public synchronized void setName(String name) {
this.name = name;
}
public String getName() { // Noncompliant
return this.name;
}
public void setAge(int age) { // Noncompliant
this.age = age;
}
public int getAge() {
synchronized (this) {
return this.age;
}
}
}
Compliant solution
public class Person {
String name;
int age;
public synchronized void setName(String name) {
this.name = name;
}
public synchronized String getName() {
return this.name;
}
public void setAge(int age) {
synchronized (this) {
this.age = age;
}
}
public int getAge() {
synchronized (this) {
return this.age;
}
}
}
Resources
Documentation
Standards
- CERT, VNA01-J. - Ensure visibility of shared references to immutable objects
Articles & blog posts