Why would shutdown immediately after creating newSingleThreadScheduledExecutor?

adrhc :

projectreactor 3.3.1 have the class reactor.core.scheduler.ElasticScheduler which contains this code:

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

Why would one do this?

An ExecutorService can be shut down, which will cause it to reject new tasks so the above created ScheduledExecutorService would become useless.

Michael Berry :

...the above created ScheduledExecutorService would become useless.

Useless if you actually wanted to submit tasks to it to be executed, yes.

Why would one do this?

Quite simply, to obtain an executor service that always rejects new tasks submitted to it, never executing any of them. In the current implementation of ElasticScheduler, it's used for the SHUTDOWN field, which is the CachedService returned by the pick() method after the enclosing scheduler is disposed.

In this use case therefore it makes perfect sense, as you explicitly don't want any new tasks to be scheduled after dispose() is called.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358856&siteId=1