Created
February 23, 2017 01:50
-
-
Save ueokande/5570389a13c390e57b09de8fffb87eb9 to your computer and use it in GitHub Desktop.
Timeout in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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