How to wait for a ThreadPoolExecutor to finish

后端 未结 6 1711
渐次进展
渐次进展 2020-12-13 18:15

My Question: How to execute a bunch of threaded objects on a ThreadPoolExecutor and wait for them all to finish before moving on?

I\'m new to Thread

相关标签:
6条回答
  • 2020-12-13 18:50

    Another approach is to use CompletionService, very useful if you have to attempt any task result:

    //run 3 task at time
    final int numParallelThreads = 3;
    
    //I used newFixedThreadPool for convenience but if you need you can use ThreadPoolExecutor
    ExecutorService executor = Executors.newFixedThreadPool(numParallelThreads);
    CompletionService<String> completionService = new ExecutorCompletionService<String>(executor);
    
    int numTaskToStart = 15;
    
    for(int i=0; i<numTaskToStart ; i++){
        //task class that implements Callable<String> (or something you need)
        MyTask mt = new MyTask();
    
        completionService.submit(mt);
    }
    
    executor.shutdown(); //it cannot be queued more task
    
    try {
        for (int t = 0; t < numTaskToStart ; t++) {
            Future<String> f = completionService.take();
            String result = f.get();
            // ... something to do ...
        }
    } catch (InterruptedException e) {
        //termination of all started tasks (it returns all not started tasks in queue)
        executor.shutdownNow();
    } catch (ExecutionException e) {
        // ... something to catch ...
    }
    
    0 讨论(0)
  • 2020-12-13 18:51

    It's nothing to do with the executor itself. Just use the interface's java.util.concurrent.ExecutorService.invokeAll(Collection<? extends Callable<T>>). It will block until all the Callables are finished.

    Executors are meant to be long-lived; beyond the lifetime of a group of tasks. shutdown is for when the application is finished and cleaning up.

    0 讨论(0)
  • 2020-12-13 18:51

    Here's a variant on the accepted answer that handles retries if/when InterruptedException is thrown:

    executor.shutdown();
    
    boolean isWait = true;
    
    while (isWait)
    {
        try
        {             
            isWait = !executor.awaitTermination(10, TimeUnit.SECONDS);
            if (isWait)
            {
                log.info("Awaiting completion of bulk callback threads.");
            }
        } catch (InterruptedException e) {
            log.debug("Interruped while awaiting completion of callback threads - trying again...");
        }
    }
    
    0 讨论(0)
  • 2020-12-13 19:03

    Try this,

    ThreadPoolExecutor ex =
        new ThreadPoolExecutor(limit, limit, 20, TimeUnit.SECONDS, q);
    for (int i = 0; i < limit; i++) {
      ex.execute(new RunnableObject(i + 1));
    }
    

    Lines to be added

    ex.shutdown();
    ex.awaitTermination(timeout, unit)
    
    0 讨论(0)
  • 2020-12-13 19:11

    You should loop on awaitTermination

    ExecutorService threads;
    // ...
    // Tell threads to finish off.
    threads.shutdown();
    // Wait for everything to finish.
    while (!threads.awaitTermination(10, TimeUnit.SECONDS)) {
      log.info("Awaiting completion of threads.");
    }
    
    0 讨论(0)
  • 2020-12-13 19:11

    Your issue seems to be that you are not calling shutdown after you have submitted all of the jobs to your pool. Without shutdown() your awaitTermination will always return false.

    ThreadPoolExecutor ex =
        new ThreadPoolExecutor(limit, limit, 20, TimeUnit.SECONDS, q);
    for (int i = 0; i < limit; i++) {
      ex.execute(new RunnableObject(i + 1));
    }
    // you are missing this line!!
    ex.shutdown();
    ex.awaitTermination(2, TimeUnit.SECONDS);
    

    You can also do something like the following to wait for all your jobs to finish:

    List<Future<Object>> futures = new ArrayList<Future<Object>>();
    for (int i = 0; i < limit; i++) {
      futures.add(ex.submit(new RunnableObject(i + 1), (Object)null));
    }
    for (Future<Object> future : futures) {
       // this joins with the submitted job
       future.get();
    }
    ...
    // still need to shutdown at the end
    ex.shutdown();
    

    Also, because you are sleeping for 2354 milliseconds but only waiting for the termination of all of the jobs for 2 SECONDS, awaitTermination will always return false. I would use Long.MAX_VALUE to wait for the jobs to finish.

    Lastly, it sounds like you are worrying about created a new ThreadPoolExecutor and you instead want to reuse the first one. Don't be. The GC overhead is going to be extremely minimal compared to any code that you write to detect if the jobs are finished.


    To quote from the javadocs, ThreadPoolExecutor.shutdown():

    Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

    In the ThreadPoolExecutor.awaitTermination(...) method, it is waiting for the state of the executor to go to TERMINATED. But first the state must go to SHUTDOWN if shutdown() is called or STOP if shutdownNow() is called.

    0 讨论(0)
提交回复
热议问题