wait until all threads finish their work in java

后端 未结 16 1956
情深已故
情深已故 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:54

    You can join to the threads. The join blocks until the thread completes.

    for (Thread thread : threads) {
        thread.join();
    }
    

    Note that join throws an InterruptedException. You'll have to decide what to do if that happens (e.g. try to cancel the other threads to prevent unnecessary work being done).

提交回复
热议问题