Created
July 20, 2018 07:12
-
-
Save daniilgri/c801828f7c96ede226d165a10d68622c to your computer and use it in GitHub Desktop.
This file contains 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
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