使用Allof的Java完整图:如果一个线程引发异常,则如何立即停止执行所有线程?

发布于 2025-01-26 14:08:19 字数 801 浏览 4 评论 0 原文

// assume: serviceCall1 throws an exception after 1s, servserviceCall2 runs 10s without exception
CompletableFuture<String> serviceCall1Future = serviceCall1.execute();
CompletableFuture<String> serviceCall2Future = serviceCall2.execute();

CompletableFuture<Void> allOffFuture = CompletableFuture.allOf(serviceCall1Future, serviceCall2Future);

// does not work, will be called after thread 2 has finished
allOffFuture.exceptionally( ex -> { allOffFuture.cancel(true); return null; } );

try {
   // waiting for threads to finish
   allOffFuture.join();
} catch (CompletionException e) {
   // does not work, here we come after thread 2 has finished
   allOffFuture.cancel(true);
}

如果一个线程引发异常,则在我的情况下,对于其他线程保持运行没有任何意义,因此我希望它们俩(在2个以上的线程中)停止。我该如何实现?

// assume: serviceCall1 throws an exception after 1s, servserviceCall2 runs 10s without exception
CompletableFuture<String> serviceCall1Future = serviceCall1.execute();
CompletableFuture<String> serviceCall2Future = serviceCall2.execute();

CompletableFuture<Void> allOffFuture = CompletableFuture.allOf(serviceCall1Future, serviceCall2Future);

// does not work, will be called after thread 2 has finished
allOffFuture.exceptionally( ex -> { allOffFuture.cancel(true); return null; } );

try {
   // waiting for threads to finish
   allOffFuture.join();
} catch (CompletionException e) {
   // does not work, here we come after thread 2 has finished
   allOffFuture.cancel(true);
}

If one thread throws an exception, in my case it doesnt make any sense for the other thread(s) to keep on running, so I want them both (all in case of more than 2 threads) to stop . How can I achieve that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2025-02-02 14:08:19

我想这样的事情应该可以起作用:

CompletableFuture<String> serviceCall1Future = serviceCall1.execute();
CompletableFuture<String> serviceCall2Future = serviceCall2.execute();

CompletableFuture<String> foo1 = serviceCall1Future.whenComplete((result,exception) -> {if(exception != null) serviceCall2Future.cancel(true);});
CompletableFuture<String> foo2 = serviceCall2Future.whenComplete((result,exception) -> {if(exception != null) serviceCall1Future.cancel(true);});

CompletableFuture<Void> allOffFuture = CompletableFuture.allOf(foo1, foo2);

// ... rest of your code

当一个人完成时,这会取消另一个未来。

I guess something like this should work:

CompletableFuture<String> serviceCall1Future = serviceCall1.execute();
CompletableFuture<String> serviceCall2Future = serviceCall2.execute();

CompletableFuture<String> foo1 = serviceCall1Future.whenComplete((result,exception) -> {if(exception != null) serviceCall2Future.cancel(true);});
CompletableFuture<String> foo2 = serviceCall2Future.whenComplete((result,exception) -> {if(exception != null) serviceCall1Future.cancel(true);});

CompletableFuture<Void> allOffFuture = CompletableFuture.allOf(foo1, foo2);

// ... rest of your code

This cancels the other future when the one completes with an exception.

撩人痒 2025-02-02 14:08:19

如果您使用具有完整future的执行权限服务,则可以使用shutdowns方法(例如shutdown()或shutdownnow())。

如果要立即关闭执行人员服务,则可以调用shutdownnow()方法。这将尝试立即停止所有执行任务,并跳过所有提交但未经处理的任务。执行任务没有保证。也许他们停下来,也许是执行直到最后。这是最好的努力。这是调用executorService shutdownnow()

的示例

请参见 - &gt;

If you are using an ExecutorService with CompletableFuture, you can use Shutdowns methods like shutdown() or shutdownNow().

If you want to shut down the ExecutorService immediately, you can call the shutdownNow() method. This will attempt to stop all executing tasks right away, and skips all submitted but non-processed tasks. There are no guarantees given about the executing tasks. Perhaps they stop, perhaps the execute until the end. It is a best effort attempt. Here is an example of calling ExecutorService shutdownNow()

See -> https://jenkov.com/tutorials/java-util-concurrent/executorservice.html#executorservice-shutdown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文