Java 执行者在任务完成前退出

Java Executors exit before task completion

        public class MyCallable implements Callable<Boolean> {        
            @Override
            public Boolean call() {
              while(true){
                 try{
                  [... stuff]
                 }catch(Exception e){
                   System.out.println("Error!");
                 }
              }
            }
        }

    public static void main(String[] args) throws Exception {
                int parallelCallables = 6;        
                ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                        .setNameFormat("Grabber-thread-%d").build();
                facebookInfoGrabberExecutor = Executors.newFixedThreadPool(parallelCallables, namedThreadFactory);
                List<Future<Boolean>> futures = facebookInfoGrabberExecutor.invokeAll(tasks);
                System.out.println("unreachable");
    }

尽管我的程序似乎不可能打印 "unreachable"。 当它到达那条线时,所有进程都处于等待状态。 我使用 hazelcast,这可能是罪魁祸首,但我仍然不知道。

您需要关闭您的 Executor 以使其完成所有任务。

    int parallelCallables = 6;
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("Grabber-thread-%d").build();
    ExecutorService facebookInfoGrabberExecutor = Executors.newFixedThreadPool(parallelCallables, namedThreadFactory);
    List<Future<Boolean>> futures = facebookInfoGrabberExecutor.invokeAll(tasks);
    // Add this!!!
    facebookInfoGrabberExecutor.shutdown();
    while (!facebookInfoGrabberExecutor.awaitTermination(1, TimeUnit.MINUTES)) {
        System.out.println("Waiting");
    }
    System.out.println("All done.");