执行程序池未处理所有项目

Executor pool not processing all items

我需要执行 5000 万项操作。我写了下面的代码

 AtomicInteger failCounter = new AtomicInteger(0);
 long start = System.currentTimeMillis();
 ExecutorService es = Executors.newFixedThreadPool(30);

  List<String> allids = getItems();//50 million items from db

  log.info(getAction() + " Total items found: " + allids.size());

  allids.stream().forEach(s -> {
    es.execute(new MyRunnable(s, failCounter));
  });

  es.shutdownNow();
  try {
    if (!es.awaitTermination(100, TimeUnit.SECONDS)) {
      System.out.println("Still waiting...");
      System.exit(0);
    }
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  System.out.println("Exiting normally...");
  log.info("counter: " + failCounter.get());

public class MyRunnable implements Runnable {

    private final String id;
    private final AtomicInteger failCounter;

    RollupRunnable(String id, AtomicInteger failCounter) {
      this.id = id;
      this.failCounter = failCounter;
    }

    @Override
    public void run() {
      try {
        //perform some action        
      } catch (Exception exception) {
        failCounter.getAndIncrement();
        log.error(
            "Error in calling " + getAction() + " for id: " + id + "  of :" + this.getClass()
                .getSimpleName(),
            exception);
      }

    }
  }

但是执行器在处理前 30 个项目后存在。

我是不是做错了什么。

而不是 es.shutdownNow(); 使用 es.shutdown();

shutDownNow() 停止所有任务的处理,包括那些甚至没有执行的任务。

这就是为什么不是所有项目都由 Executor 框架执行的原因。