Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- }
Add Comment
Please, Sign In to add comment