Created
June 7, 2013 18:50
-
-
Save maggandalf/5731495 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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