4 ms·
I like the author's original example the best, but: while ((message = map.get(key)) == null) { long now = System.currentTimeMillis(); if (now >
by MathMonkeyMan 2y ago
I like the author's original example the best, but:
while ((message = map.get(key)) == null) {
long now = System.currentTimeMillis();
if (now >= timeoutTime) {
break;
}
wait(timeoutTime - now);
}
If I had my way, then all "sleep" APIs would take a deadline time point, not a time duration. Also have the API do nothing if the deadline is passed. This way, getting the subtraction right need only happen in the lowest shared part of the library. Then it's a lot like the original example:
while ((message = map.get(key)) == null) {
waitUntil(timeoutTime);
}
edit: Oops, my last example has a pretty bad bug. The author's point stands, and I need coffee.
You could have waitUntil return true or false to indicate whether the wait occurred or not, but then that loses the opportunity to convey whether the thread was awakened due to an operation on the map.
do {
message = map.get(key);
} while (message == null && waitUntil(timeoutTime));
Author's original example still wins.