0

I am using executor service to run my 10 tasks with 2 tasks at a time.

ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; i++) {
    String name = "NamePrinter " + i;
    Runnable runner = new TaskPrint(name, 1000);
    System.out.println("Adding: " + name + " / " + 1000);
    executor.execute(runner);
}

How can I wait for all tasks to complete

Jobs
  • 1,257
  • 2
  • 14
  • 27
  • 1
    https://stackoverflow.com/questions/19348248/waiting-on-a-list-of-future – Pooya May 07 '18 at 18:27
  • Yes, I saw the question, but adding this line stopped the program immediately taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { ... } also, I don't know to tell max wait time – Jobs May 07 '18 at 18:32
  • Then put up a [mcve]. But I am guessing that you call that method inside your loop?! – GhostCat May 07 '18 at 18:34
  • 1
    https://stackoverflow.com/questions/19348248/waiting-on-a-list-of-future The taskExecutor.shutdown(); in the answer is stopping the program. I don't know why 300 + people upvoted the answer – Jobs May 07 '18 at 18:44

2 Answers2

3

Use java 8 CompleteableFuture with join method to wait:

ExecutorService executor = Executors.newFixedThreadPool(2);
CompletableFuture[] futures = new CompletableFuture[10];
for (int i = 0; i < 10; i++) {
    String name = "NamePrinter " + i;
    Runnable runner = new TaskPrint(name, 1000);
    System.out.println("Adding: " + name + " / " + 1000);
    futures[i] =  CompletableFuture.runAsync(runner, executor);

}
CompletableFuture.allOf(futures).join(); // THis will wait until all future ready.
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0

Assign your callables to futures and check that you can get results from each future.

        Future future = workerExecutor.submit(new Callable() {
            @Override
            public Object call() throws Exception {
                try {
                    System.out.println("MyItemTree.TimedRunnable");
                    ReturnInterface returnInterface = (ReturnInterface) commandInterface.call();
                    returnInterface.submitResult();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw new RuntimeException(ex);
                }
                return null;
            }
        });

        try {
            Object get = future.get();
        } catch (InterruptedException | ExecutionException ex) {
            Throwable cause = ex.getCause();

            ex.printStackTrace();
            cause.printStackTrace();
            Throwable cause1 = cause.getCause();

            if (cause1 instanceof CommandInterfaceException) {
                System.out.println("[MyItemTree].scheduleTask Cause 1= COMMANDINTERFACE EXCEPTION");
                this.componentInterface.getAlertList().addAlert(((CommandInterfaceException) cause1).getResolverFormInterface());
            }
        }

    }
CoupFlu
  • 311
  • 4
  • 20