为什么创建newSingleThreadScheduledExecutor后会立即关闭?

Why would shutdown immediately after creating newSingleThreadScheduledExecutor?

projectreactor 3.3.1 有 class reactor.core.scheduler.ElasticScheduler 其中包含此代码:

this.exec = Executors.newSingleThreadScheduledExecutor();
this.exec.shutdownNow();

为什么要这样做?

An ExecutorService can be shut down, which will cause it to reject new tasks so the above created ScheduledExecutorService就没用了

...the above created ScheduledExecutorService would become useless.

如果你真的想提交任务给它执行,是没用的,是的。

Why would one do this?

很简单,要获得一个 总是 拒绝提交给它的新任务,从不执行任何任务的执行器服务。在 ElasticScheduler 的当前实现中,它用于 SHUTDOWN 字段,这是 pick() 方法在处理封闭调度程序后返回的 CachedService

因此在这个用例中它非常有意义,因为您明确不希望在调用 dispose() 之后安排任何新任务。