spotbugs / spotbugs

SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.

Home Page:https://spotbugs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deadlock issues with non-monotonic time

DaveJarvis opened this issue · comments

There are potential deadlocks that I've encountered across a few code bases now. The reduced form resembles:

long start = System.currentTimeMillis();
boolean blocked = System.currentTimeMillis() - start < TIMEOUT;

If the system time changes non-linearily (e.g., NTP daemon updates the time), then it's possible that the code will block indefinitely, or cause loops to terminate unexpectedly.

The recommended alternative is to use the monotonic System.nanoTime() for blocking loops.

Would this make for a possible check to add to SpotBogs?