How to run task in batches using ExecutorService

Viewed 118

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?

1 Answers

Modulo

Use the modulo operator % to determine every 8th number:

if( ( i % 8 ) == 0 ) { … }

Modulo gives the remainder after division. So a number that is an even multiple of 8 will be divisible by eight with a remainder of zero. If a number modulo eight is zero, we have an even multiple.

Also, you forgot to wait for tasks to complete. Call awaitTermination on your executor service.

ExecutorService executorService = Executors.newFixedThreadPool(8);

for ( int i = 1 ; i <= filesInDirectory ; i++ ) {
    executorService.submit( new Importer( service ) );
    // On every 8th loop, sleep.
    if( ( i % 8 ) = 0 ) {
        Thread.sleep( Duration.ofSeconds( 10 ).toMillis() ) ;
    }
}

executorService.shutdown();                          // Ask service to shut down after all submitted tasks are done/failed/canceled.
executorService.awaitTermination( … , TimeUnit.… );  // Flow-of-control blocks, waiting until all submitted tasks are done/failed/canceled.

Be aware that your eight submitted tasks may or may not be done after waiting the ten seconds. The background threads make their own progress on their own schedule without regard to this thread being asleep or not. If the previous 8 tasks submitted are not yet done/failed/canceled, the executor service will buffer up the next group of 8 submitted tasks until a thread becomes available.

Related