如何等待多个线程完成?

如何等待多个线程完成?

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

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
    }}

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



哔哔one
浏览 459回答 3
3回答

慕田峪7331174

您将所有线程放在一个数组中,启动它们,然后循环for(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;threads.length;&nbsp;i++) &nbsp;&nbsp;threads[i].join();每个连接将阻塞,直到相应的线程完成。线程可以以与加入它们不同的顺序完成,但这不是问题:当循环退出时,所有线程都完成。

叮当猫咪

一种方法是做一个List的ThreadS,创建和启动每个线程,而将其添加到列表中。一旦启动所有内容,循环回列表并调用join()每个列表。线程完成执行的顺序并不重要,您需要知道的是,当第二个循环完成执行时,每个线程都将完成。更好的方法是使用ExecutorService及其相关方法:List<Callable> callables = ... // assemble list of Callables here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Like Runnable but can return a valueExecutorService execSvc = Executors.newCachedThreadPool();List<Future<?>> results = execSvc.invokeAll(callables);// Note: You may not care about the return values, in which case don't//&nbsp; &nbsp; &nbsp; &nbsp;bother saving them使用ExecutorService(以及来自Java 5的并发实用程序的所有新东西)非常灵活,上面的示例几乎没有表面上的划痕。

长风秋雁

import&nbsp;java.util.ArrayList;import&nbsp;java.util.List;import&nbsp;java.util.concurrent.ExecutionException;import&nbsp;java.util.concurrent.ExecutorService;import&nbsp;java.util.concurrent.Executors;import&nbsp;java.util.concurrent.Future;public&nbsp;class&nbsp;DoSomethingInAThread&nbsp;implements&nbsp;Runnable{ &nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;ExecutionException,&nbsp;InterruptedException &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//limit&nbsp;the&nbsp;number&nbsp;of&nbsp;actual&nbsp;threads &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;poolSize&nbsp;=&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExecutorService&nbsp;service&nbsp;=&nbsp;Executors.newFixedThreadPool(poolSize); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<Future<Runnable>>&nbsp;futures&nbsp;=&nbsp;new&nbsp;ArrayList<Future<Runnable>>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;n&nbsp;=&nbsp;0;&nbsp;n&nbsp;<&nbsp;1000;&nbsp;n++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Future&nbsp;f&nbsp;=&nbsp;service.submit(new&nbsp;DoSomethingInAThread()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;futures.add(f); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;wait&nbsp;for&nbsp;all&nbsp;tasks&nbsp;to&nbsp;complete&nbsp;before&nbsp;continuing &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Future<Runnable>&nbsp;f&nbsp;:&nbsp;futures) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.get(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//shut&nbsp;down&nbsp;the&nbsp;executor&nbsp;service&nbsp;so&nbsp;that&nbsp;this&nbsp;thread&nbsp;can&nbsp;exit &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service.shutdownNow(); &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;run() &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something&nbsp;here &nbsp;&nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java