简体   繁体   中英

Java executor service: Waiting for all tasks to finish

I am trying to introduce concurrency in my program. Structure of program is something like this:

    ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
    List<String> initialData = dao.fetchFromDB("input");
    Queue queue = new MyQueue();
    queue.add(initialData);

    while(queue.length() > 0) {
        int startingLength = queue.length();
        for (int i = 0; i < startingLength; i++) {
            String input = queue.remove();
            if(input.equals("some value")) {
              missionAccomplished = true;
              break;
            } else {
                  MyRunnable task = new MyRunnable(input, queue, dao);
                  executor.execute(task);
            }
            
        }
        if(missionAccomplished) {
           break;
        } 
        executor.shutdown();
    }
 

So queue contains the data needed to be processed one by one. Inside while loop I run a for loop which picks data from queue one by one sequentially and performs some check on it, and if check fails I create a runnable task with this data and hands it over to executor(as DB operation is time consuming, I want to use parallelism for it). for loop picks data only upto certain length in given iteration of while. What I want to achieve is that 'while' loop goes to next iteration only when all tasks submitted to executor in current iteration are finished.

How can this be achieved?

try-with-resources in Project Loom

You asked:

What I want to achieve is that 'while' loop goes to next iteration only when all tasks submitted to executor in current iteration are finished.

Project Loom promises to make this simpler.

One of the changes brought by Project Loom is that the ExecutorService interface is a sub-interface of AutoCloseable . This means we can use try-with-resources syntax. The try-with-resources automatically blocks until all submitted tasks are done /failed/canceled — just what you asked for.

Also, the executor service is automatically shut down when exiting the try . These changes mean your code becomes simpler and clearer.

Also, for code that blocks often, such as database access, you will see dramatically faster performance using virtual threads (aka fibers ). Virtual threads is another new feature of Project Loom. To get this feature, call Executors.newVirtualThreadExecutor .

Experimental builds of Project Loom are available now , based on early-access Java 17. The Loom team is asking for feedback. For more info, see recent presentations and interviews by Ron Pressler of Oracle.

System.out.println( "INFO - executor service about to start. " + Instant.now() );
try (
        ExecutorService executorService = Executors.newVirtualThreadExecutor() ;
)
{
    for ( int i = 0 ; i < 7 ; i++ )
    {
        executorService.submit( ( ) -> System.out.println( Instant.now() ) );
    }
}
// Notice that when reaching this point we block until all submitted tasks still running are fin
// because that is the new behavior of `ExecutorService` being `AutoCloseable`.
System.out.println( "INFO - executor service shut down at this point. " + Instant.now() );

When run.

INFO - executor service about to start. 2021-02-08T06:27:03.500093Z
2021-02-08T06:27:03.554440Z
2021-02-08T06:27:03.554517Z
2021-02-08T06:27:03.554682Z
2021-02-08T06:27:03.554837Z
2021-02-08T06:27:03.555015Z
2021-02-08T06:27:03.555073Z
2021-02-08T06:27:03.556675Z
INFO - executor service shut down at this point. 2021-02-08T06:27:03.560723Z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM