In some cases, most of us write things like this:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
; // do nothing
}
Whether right or wrong, acceptable only in some test harnesses, is not my point.
My point is that the same code could be written,more succinctly, as:
LockSupport.parkNanos(2000* 1000000);
is there any reason why I should favour one approach over the other.
The docs for the method
parkNanosprovides the conditions in which the method can return. One of those conditions is: the call spuriously (that is, for no reason) returns. So basically it’s OK to use it if you don’t mind spurious wake-ups and some other Thread “unparking” the waiting thread in consideration. And of course, the comment by Jon pretty much nails the reasoning for preferring one over another.