A rookie mistake
Deepak Sarda
CTO, Endowus | Ex-AWS, JPMorgan Chase | SG Digital Leader 2024 | MIT-SMA, IIT Madras
Consider the following function :
Can you spot the error in this function?
It makes the rookie mistake of assuming that `sleep()` is reliable. The call to `TimeUnit.SECONDS.sleep(1)` will certainly put the thread to sleep for a second, that much we can rely upon. The problem is that the sleep can extend to beyond one second; perhaps 2 seconds on an autumn day and perhaps 5 seconds on a full moon night.
Essentially, there are no guarantees that the duration of sleep will be exact or even within a tolerance. This makes the `checkTimeOut` a lower bound at best and a long lasting siesta at worst.
A recent incident at work was traced in part to a function that, in essence, made the same mistake. It ran fine for eight months in production before failing. Since I was on support during this incident and helped diagnose the root cause, I naturally went back to see who wrote the code and when.
No prizes for guessing who it was ???
Director - Supply Chain & Operations
9 年So true!