繁体   English   中英

Java等待所有线程完成

[英]Java Waiting for all threads to complete

编辑:我的问题是不同的,它与链接的问题无关。

我正在用完成处理程序跟踪代码。

FutureTask<Void> futureTask = new FutureTask<Void>(() -> {
    System.out.println("callback");
    return null;
});

Runnable task = () -> {
    for(int i=0; i<5; i++) {
        System.out.println(Thread.currentThread().getName() + " " + i);
    }
    futureTask.run();
};

new Thread(task).start();
new Thread(task).start();

基本上,我正在寻找可变数量任务的完成处理程序,还是有另一种方法?

我从这个答案中得到启发,但是当我在寻找本机解决方案时,它似乎是某些库的一部分。

可完成的未来

这是我尝试使用最终结果处理程序完成的期货。

public void test() {
    CompletableFuture
            .supplyAsync(() -> method1())
            .supplyAsync(() -> method2())
            .supplyAsync(() -> result());
}

public String method1() {
    System.out.println("calling 1");
    return "method1";
}

public String method2() {
    System.out.println("calling 2");
    return "method2";
}

public String result() {
    System.out.println("result");
    return "result";
}

假设您的方法result()返回要检索的值,即像Type result()那样声明,则可以使用

CompletableFuture<Type> f = CompletableFuture.allOf(
    CompletableFuture.runAsync(() -> method1()),
    CompletableFuture.runAsync(() -> method2())
).thenApply(_void -> result());

每个runAsync创建一个单独的异步CompletableFuture ,一旦执行了Runnable ,该异步CompletableFuture将完成。 除了不返回结果外,它与supplyAsync相同。

allOf创建一个CompletableFuture ,一旦所有指定的期货都完成,该CompletableFuture将完成,因此,任何相关的连锁操作都将在所有期货都已完成之后运行。 通过使用thenApply我们创建了一个依存的thenApply ,它将以result()的返回值完成。

如果result()并非要返回值,而只是要在所有其他动作完成后才运行的动作,则可以使用

CompletableFuture.allOf(
    CompletableFuture.runAsync(() -> method1()),
    CompletableFuture.runAsync(() -> method2())
).thenRun(() -> result());

代替。

一种简单的方法是将您的Runnable提交给ExecutorService,然后调用shutdown ,然后调用awaitTermination

ExecutorService executor = Executors.newWorkStealingPool();
executor.submit(task);
executor.submit(task);

executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

无需使用CompletableFuture。

保存到您创建的线程的链接,然后调用join()

Thread a = new Thread(task);
Thread b = new Thread(task);
a.start();
b.start();

a.join();
b.join();
//guaranteed that both threads have completed here

根据您想要多少控制权,您可以使用ThreadPoolExecutor:

tpe.execute(Runnable);

等待有效计数== 0;

然后关闭执行器。

或者将线程保持在一个结构中。

等待特定的TTL,然后在状态为RUNNABLE时中断它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM