OPT.SYN
Never call a "synchronized" method in a loop
Description
This rule flags any "synchronized" method that is invoked inside a loop.
Invoking a "synchronized" method is expensive. Thus, they should not be invoked inside of a loop.
Example
package OPT;
import java.util.Vector;
public class SYN {
public synchronized void method (Object o) {
}
private void method () {
for (int i = 0; i < vector.size(); i++) {
method (vector.elementAt(i)); // violation
}
}
private Vector vector = new Vector (5, 5);
}
Repair
Do not invoke a "synchronized" method in the loop body.
|