Executor 代替 ExecutorService 有没有什么场景。 Executor 接口背后的意图?

Is there any scenario for Executor instead of ExecutorService. Intention behind Executor interface?

我想知道是否有任何理由使用 Executor 而不是 ExecutorService。

据我所知,JDK 中没有实现 Executor 接口,它也不是 ExecutorService,这意味着您必须关闭该服务,以便没有内存泄漏。您无法关闭 Executor,但可以使用 ExecutorService 关闭。

那么,有没有什么场景可以使用类似的东西:

private final Executor _executor = Executors.newCachedThreadPool();

Executor界面背后的意图是什么?示例表示赞赏。

JavaDoc 在这种情况下是你的朋友

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

往下看

However, the Executor interface does not strictly require that execution be asynchronous. In the simplest case, an executor can run the submitted task immediately in the caller's thread:

 class DirectExecutor implements Executor {
     public void execute(Runnable r) {
         r.run();
     }
 }

这些定义对我来说已经很能表达了。

关于您的用法示例。恕我直言,不,我永远不会那样使用这个界面。
但是它可能被用作接受的论点。

例如我想 运行 一个任务,但我希望我的方法的用户决定这个任务如何 运行。

public void run(final Executor taskExecutor) {
   taskExecutor.execute(this.myTaskRunnable);
}

Executor 实现可能在幕后,它可能是同步的(如上面的示例)或异步的,但我并不关心。
它是 user 将负责处理它。