Skip to content

Instantly share code, notes, and snippets.

@ueokande
Created February 23, 2017 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueokande/5570389a13c390e57b09de8fffb87eb9 to your computer and use it in GitHub Desktop.
Save ueokande/5570389a13c390e57b09de8fffb87eb9 to your computer and use it in GitHub Desktop.
Timeout in java
String synchronizedHeavyMethod(Duration timeout) throws TimeoutException {
Instant deadline = Instant.now().plus(timeout);
while (Instant.now().isBefore(deadline)) {
// doSomething() resutrns nullable String
String result = doSomething();
if (Objects.isNull(result)) {
continue;
}
return result;
}
throw new TimeoutException("timeout");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment