How to wait for a ThreadPoolExecutor to finish

后端 未结 6 1721
渐次进展
渐次进展 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 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)
    

提交回复
热议问题