wait until all threads finish their work in java

后端 未结 16 2167
情深已故
情深已故 2020-11-22 14:10

I\'m writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class.
I need to validate buffer

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 14:56

    The approach I take is to use an ExecutorService to manage pools of threads.

    ExecutorService es = Executors.newCachedThreadPool();
    for(int i=0;i<5;i++)
        es.execute(new Runnable() { /*  your task */ });
    es.shutdown();
    boolean finished = es.awaitTermination(1, TimeUnit.MINUTES);
    // all tasks have finished or the time has been reached.
    

提交回复
热议问题