I'm trying to run 8 tasks in parallel, then sleep for some time, then run new tasks again? How can I achieve that?
I tried using ExecutorService but I do not seem to get the proper result.
I have 1000 files, and each task should read one file, and save its content to the database.
This does not seem to work:
ExecutorService executorService = Executors.newFixedThreadPool(8);
for ( int i = 1 ; i <= filesInDirectory ; i++ ) {
executorService.submit( new Importer( service ) );
Thread.sleep( Duration.ofSeconds( 10 ).toMillis() ) ;
}
executorService.shutdown();
I want the executor to run all 8 threads at the same time, then sleep 10s, and start again with 8 threads again.
How to achieve that?