Created
November 19, 2015 15:37
-
-
Save mathieucarbou/e5e38f094670a944d2f9 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
private static <T> T callInOtherThread(Callable<T> task) throws Throwable { | |
ExecutorService service = Executors.newSingleThreadExecutor(); | |
try { | |
Future<T> future = service.submit(task); | |
boolean interrupted = false; | |
try { | |
while (true) { | |
try { | |
return future.get(); | |
} catch (InterruptedException e) { | |
interrupted = true; | |
} catch (ExecutionException e) { | |
throw e.getCause(); | |
} | |
} | |
} finally { | |
if (interrupted) { | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} finally { | |
service.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment