TRS.WAIT
The condition test should always be in a loop
Description
This rule flags any condition test that is not inside a loop.
The condition test should always be in a loop. You cannot assume that a thread being awakened indicates that the condition has been satisfied. "if"(cond) statement should never be used for condition test; use "while"(cond) statement.
Example
package TRS;
public class WAIT {
synchronized void method(boolean isWait) {
if(isWait) { // should be a "while" statement.
wait();
}
}
}
Repair
Replace "if(condition)" with "while(condition)" statement.
Reference
Arnold, Ken and Gosling, James. The Java Programming Language 2nd ed. Addison Wesley, 1997, pp.188-190.
|