不要等待完整的future

发布于 2025-01-22 22:16:16 字数 503 浏览 3 评论 0原文

假设我有一些我想在单独的线程中执行的逻辑,然后继续使用方法中的剩余逻辑。

如果我不关心单独线程中逻辑的结果(可以例外地完成,可以通过结果完成),我可以通过postectablefuture.runasync来执行逻辑吗?只是为了重申,我不需要关于在单独线程中执行的状态或完成逻辑的任何信息。

例如,

public void doSomething() {
  // Don't care about the result of doSomethingElse - only care that execution starts and will complete with some outcome
  CompletableFuture.runAsync(() -> doSomethingElse());
  // do more things synchronously
  return;
}

Let's say I have some logic that I want to execute in a separate thread, and then continue with the remaining logic in my method.

If I don't care about the result of the the logic in the separate thread (could complete exceptionally, could complete with a result), can I simply execute the logic via CompletableFuture.runAsync? Just to reiterate, I do not need any information about the state or completion of the logic executed in the separate thread.

For example,

public void doSomething() {
  // Don't care about the result of doSomethingElse - only care that execution starts and will complete with some outcome
  CompletableFuture.runAsync(() -> doSomethingElse());
  // do more things synchronously
  return;
}

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

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

发布评论

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

评论(1

冷血 2025-01-29 22:16:16

您可以,但是要当心 - 如果您的程序在完成此完整future之前完成运行,那么完整的future可能会被关闭。这很重要,因为只是在中途停止执行的程序可能会导致一些难以捉摸且难以调试错误。因此,实际上是3个可能的终端状态,而不是2个状态

  1. 。除非您围绕此明确设计,否则完成
  2. 在完成之前,在完成之前完全
  3. 停止,没有错误或通知。

You can, but beware - if your program finishes running before this CompletableFuture completes, then the CompletableFuture might just get force shut down. This is important because a program that just stops executing halfway through can cause some elusive and difficult to debug errors. So it's actually 3 possible end states instead of 2.

  1. Complete successfully
  2. Complete exceptionally
  3. Stopped before completion with no error or notification unless you explicitly designed around this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文