MSC01-J. Do not use an empty infinite loop
An infinite loop with an empty body consumes CPU cycles but does nothing. Optimizing compilers and just-in-time systems (JITs) are permitted to (perhaps unexpectedly) remove such a loop. Consequently, programs must not include infinite loops with empty bodies.
Noncompliant Code Example
This noncompliant code example implements an idle task that continuously executes a loop without executing any instructions within the loop. An optimizing compiler or JIT could remove the while loop in this example.
public int nop() {
while (true) {}
}
Compliant Solution ( Thread.sleep() )
This compliant solution avoids use of a meaningless infinite loop by invoking Thread.sleep() within the while loop. The loop body contains semantically meaningful operations and consequently cannot be optimized away.
public final int DURATION=10000; // In milliseconds
public void nop() throws InterruptedException {
while (true) {
// Useful operations
Thread.sleep(DURATION);
}
}
Compliant Solution ( yield() )
This compliant solution invokes Thread.yield() , which causes the thread running this method to consistently defer to other threads:
public void nop() {
while (true) {
Thread.yield();
}
}
Risk Assessment
| Rule | Severity | Likelihood | Detectable | Repairable | Priority | Level |
|---|---|---|---|---|---|---|
| MSC01-J | Low | Unlikely | Yes | Yes | P3 | L3 |
Automated Detection
| Tool | Version | Checker | Description |
|---|---|---|---|
| Klocwork | 2025.2 | JAVA.INF.LOOP.EMPTY | |
| Parasoft Jtest | 2025.2 | CERT.MSC01.EB | Avoid control statements with empty bodies |
| Security Reviewer - Static Reviewer | 6.02 | CWE561P1FOR | Full Implementation |
| SonarQube | 3.10 | S2189 |
Bibliography
| [ API 2014 ] |


