Skip to content

Instantly share code, notes, and snippets.

@maggandalf
Created June 7, 2013 18:50
Show Gist options
  • Save maggandalf/5731495 to your computer and use it in GitHub Desktop.
Save maggandalf/5731495 to your computer and use it in GitHub Desktop.
FutureTask<MyResult> timeoutTask = null;
try {
timeoutTask = new FutureTask<MyResult>(new Callable<MyResult>() {
@Override
public MyResult call() throws Exception {
MyResult result = //expensive operation
return result;
}
});
new Thread(timeoutTask).start();
MyResult result = timeoutTask.get(5L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} catch (TimeoutException e) {
//If time expires, (after 5 seconds) this exception is thrown.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment