Skip to content

Instantly share code, notes, and snippets.

@daniilgri
Created July 20, 2018 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniilgri/c801828f7c96ede226d165a10d68622c to your computer and use it in GitHub Desktop.
Save daniilgri/c801828f7c96ede226d165a10d68622c to your computer and use it in GitHub Desktop.
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService es1 = Executors.newFixedThreadPool(5);
Future<String> f1 = es1.submit(new Callable<String>() {
public String call() {
return "Hello";
}
});
while(!f1.isDone()) { }
try {
System.out.println("Получили " + f1.get());
} catch (InterruptedException ie) {
ie.printStackTrace(System.err);
} catch (ExecutionException ee) {
ee.printStackTrace(System.err);
} finally {
try {
System.out.println("attempt to shutdown executor");
es1.shutdown();
es1.awaitTermination(5, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
System.err.println("tasks interrupted");
}
finally {
if (!es1.isTerminated()) {
System.err.println("cancel non-finished tasks");
}
es1.shutdownNow();
System.out.println("shutdown finished");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment