如果满足特定条件如何停止线程执行

How to stop thread from execution if certain condition is met

说我有一个尝试从数据库加载数据的服务,如果数据库中不存在数据,它会尝试从主数据库加载,如果也没有找到它,它将尝试从另一个数据库中获取数据API.

这是客户提出的加载数据的业务需求。

在最坏的情况下,我们如何减少我的休息端点的响应时间?

我正在尝试使用执行程序服务框架将多个线程生成为 运行 异步,但我不知道如果其中一个线程 returns 成功响应(加载数据)。对此的任何帮助表示赞赏。

当您 submit 一个 ExecutorService 的任务时,它 returns 您 Future
只需调用 Futurecancel​() 方法即可。来自文档:

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.