Skip to content

Instantly share code, notes, and snippets.

@mathieucarbou
Created November 19, 2015 15:37
Show Gist options
  • Save mathieucarbou/e5e38f094670a944d2f9 to your computer and use it in GitHub Desktop.
Save mathieucarbou/e5e38f094670a944d2f9 to your computer and use it in GitHub Desktop.
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