相关疑难解决方法(0)

如何等待多个线程完成?

简单地等待所有线程进程完成的方法是什么?例如,假设我有:

public class DoSomethingInAThread implements Runnable{

    public static void main(String[] args) {
        for (int n=0; n<1000; n++) {
            Thread t = new Thread(new DoSomethingInAThread());
            t.start();
        }
        // wait for all threads' run() methods to complete before continuing
    }

    public void run() {
        // do something here
    }


}
Run Code Online (Sandbox Code Playgroud)

我如何改变这一点,以便main()方法在注释处暂停,直到所有线程的run()方法都退出?谢谢!

java parallel-processing multithreading wait

103
推荐指数
8
解决办法
16万
查看次数

标签 统计

java ×1

multithreading ×1

parallel-processing ×1

wait ×1