终止服务时关闭 ScheduledExecutorService

Shutdown ScheduledExecutorService when killing service

我已将 ScheduledExecutorService 用于我的 jar 文件,我正在 linux 机器上将其作为服务执行。我通过 nohup 命令声明服务并使用 kill 命令终止它。但是听说ScheduledExecutorService用完应该关机。如果没有,它将保留 JVM 运行,即使所有其他线程都已关闭。

  1. 当我终止服务时,我的 ScheduledExecutorService 线程会发生什么情况?
  2. 如果ScheduledExecutorService线程在终止服务时没有关闭,我该如何关闭它?

如果您在外部终止整个进程,那么它会同时终止 ScheduledExecutorService,因为它在进程内部运行。

不推荐这样做,因为它会在程序正在进行的任何处理过程中终止您的程序。例如,您可能有部分写入的文件。