How to wait for all threads to finish, using ExecutorService?

前端 未结 26 2414
你的背包
你的背包 2020-11-22 01:55

I need to execute some amount of tasks 4 at a time, something like this:

ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
    tas         


        
26条回答
  •  孤城傲影
    2020-11-22 02:30

    This might help

    Log.i(LOG_TAG, "shutting down executor...");
    executor.shutdown();
    while (true) {
                    try {
                        Log.i(LOG_TAG, "Waiting for executor to terminate...");
                        if (executor.isTerminated())
                            break;
                        if (executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
                            break;
                        }
                    } catch (InterruptedException ignored) {}
                }
    

提交回复
热议问题