public static < T > void parallelBatch ( Iterator < ? extends T > iterator , final Consumer < Iterator < ? extends T > > consumer , int batchSize ) throws InterruptedException , ExecutionException { List < Callable < Boolean >> callables = new ArrayList < Callable < Boolean > > ( ) ; while ( iterator . hasNext ( ) ) { Iterator < T > i = next ( iterator , batchSize ) ; final List < T > list = collect ( i ) ; callables . add ( new Callable < Boolean > ( ) { @ Override public Boolean call ( ) throws Exception { consumer . accept ( list . iterator ( ) ) ; return true ; } } ) ; } List < Future < Boolean > > futures = EXECUTOR_SERVICE . invokeAll ( callables ) ; for ( Future < Boolean > future : futures ) { future . get ( ) ; } }
Break an iterator s elements into batches and invoke the consumer on these batches in a thread pool .