Say I have a class with synchronized methods. It’s actually a class that uses Apache POI to write excel files.
public static synchronized List<CellData> createRow(final CellData... columns) {
List<CellData> row = new ArrayList<CellData>();
if (columns != null && columns.length > 0) {
for (CellData column : columns) {
row.add(column);
}
}
return row;
}
public synchronized void writeFile() throws IOException {
.................
}
This helper class resides in a jar (common library) and many programs use it at the same time.
Do I really need to make this class synchronized since every other class that uses it creates an instance of that helper class?
You only need to synchronize the access of shared data if there’s a possibility that you’ll have more than thread accessing the same instance of your class at the same time.
So the questions are:
Do your methods actually share the access to any instance variables?
Are you ever going to have more than one thread accessing one instance of your class at the same time?
In the situation you described does not seem to be something that anyone that uses that library would want to do. You could simply just document that this implementation is not synchronized.