java 退出应用程序时如何关闭所有执行程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1211657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:39:02  来源:igfitidea点击:

How to shut down all Executors when quitting an application?

javaconcurrency

提问by Joonas Pulakka

According to Brian Goetz's Java Concurrency in Practice JVM can't exit until all the (nondaemon) threads have terminated, so failing to shut down an Executor could prevent the JVM from exiting.

根据 Brian Goetz 的 Java Concurrency in Practice JVM 在所有(非守护进程)线程终止之前无法退出,因此未能关闭 Executor 可能会阻止 JVM 退出。

I.e. System.exit(0) doesn't necessarily work as expected if there are Executors around. It would seem necessary to put some kind of

如果周围有 Executors,即 System.exit(0) 不一定按预期工作。似乎有必要放置某种

public void stop() { exec.shutdown() }

methods to all classes that contain Executors, and then call them when the application is about to terminate. Is this the only way, or is there some kind of shortcut to shut down all the Executors?

包含 Executor 的所有类的方法,然后在应用程序即将终止时调用它们。这是唯一的方法,还是有某种捷径可以关闭所有 Executor?

回答by skaffman

There's no shortcut to do them all, no. Also, you should probably call shutdownNow()rather than shutdown(), otherwise you could be waiting a while.

没有捷径可以全部完成,不。此外,您可能应该致电shutdownNow()而不是shutdown(),否则您可能会等待一段时间。

What you could do, I suppose, is when you create the Executor, register it in a central place. Then, when shutting down, just call shutdown()on that central object, which in turn could terminate each of the registered executors.

我想,您可以做的是,当您创建 Executor 时,将其注册到一个中央位置。然后,在关闭时,只需调用shutdown()该中央对象,该对象又可以终止每个已注册的执行程序。

If you use Spring, then you can take advantage of its factory beans which create and manage the Executors for you. That includes shutting them down gracefully when the application quits, and saves you having to manage them yourself.

如果您使用 Spring,那么您可以利用它的工厂 bean 来为您创建和管理 Executor。这包括在应用程序退出时优雅地关闭它们,并让您不必自己管理它们。

回答by Adrian Panasiuk

Decorate the executor with com.google.common.util.concurrent.MoreExecutors#getExitingExecutorService

使用com.google.common.util.concurrent.MoreExecutors#getExitingExecutorService装饰执行

@Beta
public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor,
                                         long terminationTimeout,
                                         TimeUnit timeUnit)

Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. It does so by using daemon threads and adding a shutdown hook to wait for their completion.

This is mainly for fixed thread pools. See Executors.newFixedThreadPool(int).

将给定的 ThreadPoolExecutor 转换为在应用程序完成时退出的 ExecutorService。它通过使用守护线程并添加关闭挂钩来等待它们完成。

这主要用于固定线程池。请参阅 Executors.newFixedThreadPool(int)。

回答by Adrian Panasiuk

By default, an Executor will create only non-daemon threads. You can override that by supplying the Executor with your own ThreadFactory. Here's an example:

默认情况下,Executor 将只创建非守护线程。您可以通过为 Executor 提供您自己的 ThreadFactory 来覆盖它。下面是一个例子:

class DaemonThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r);
    t.setDaemon(true);
    return t;
  }
}

Be cautious, though, because the JVM will exit right awayeven if these threads are busy doing useful work!

要谨慎,但是,因为JVM将退出的时候了,即使这些线程都在忙着做无用功!

回答by shuckc

You can also provide an implementation of ThreadFactory that marks created threads as daemon threads. I prefer a clean shutdown mechanism (with lifecycle methods) but there are cases where you don't need guarantees about the state/completion of uncompleted tasks when this can be appropriate.

您还可以提供 ThreadFactory 的实现,将创建的线程标记为守护线程。我更喜欢干净的关闭机制(使用生命周期方法),但在某些情况下,您不需要在适当的情况下保证未完成任务的状态/完成情况。

回答by Bhushan Bhangale

Probably he meant to say JVM can't stop on its own until nondaemon threads are finished. Its like running a simple class from command like java SomeClass and after the execution of main method JVM stops.

可能他的意思是说 JVM 在非守护线程完成之前不能自行停止。它就像从像 java SomeClass 这样的命令运行一个简单的类,并且在 main 方法的执行之后 JVM 停止。

System.exit is a JVM termination command, even if daemon threads are running JVM will shutdown.

System.exit 是一个 JVM 终止命令,即使守护线程正在运行,JVM 也会关闭。