Мне нужно выполнить какое-то действие 50 миллионов предметов.Я написал ниже код
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 элементов.
Я что-то не так делаю.