Java 期货 - 急于执行?

Java futures - eager execution?

我读到 Futures 是一种异步执行任务的干净方式,记得有一次它说任务不会执行,除非你从 Future 中读取 return 值(在也许是复制懒惰评估的尝试)。我有一种感觉,这是不准确的。

有人可以澄清这是否属实吗?

如果您想执行一些异步逻辑而不想读取它的 return 值怎么办? (例如,像我在我的应用程序中所做的那样写入数据库)

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

Future就是等待一个结果。 future 对象持有对以后可用结果的引用。立即调用 Future returns,让您可以继续编写代码,而不是忙于等待答案。

如果您只想执行异步操作而不是等待结果,您可以使用线程,或者在您的情况下可能使用 Executors(或 ExecutorServices)。

Executor

An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.

ExecutorService 为一些控制功能扩展执行器。