不要等待完整的future
假设我有一些我想在单独的线程中执行的逻辑,然后继续使用方法中的剩余逻辑。
如果我不关心单独线程中逻辑的结果(可以例外地完成,可以通过结果完成),我可以通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以,但是要当心 - 如果您的程序在完成此完整future之前完成运行,那么完整的future可能会被关闭。这很重要,因为只是在中途停止执行的程序可能会导致一些难以捉摸且难以调试错误。因此,实际上是3个可能的终端状态,而不是2个状态
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.