
第一种方法
首先看下源码注释:
A pool that is no longer referenced in a program AND
has no remaining threads will be {@code shutdown} automatically. If
you would like to ensure that unreferenced pools are reclaimed even
if users forget to call {@link #shutdown}, then you must arrange
that unused threads eventually die, by setting appropriate
keep-alive times, using a lower bound of zero core threads and/or
setting {@link #allowCoreThreadTimeOut(boolean)}.
如果程序中不再持有线程池的引用,并且线程池中没有线程时,线程池将会自动关闭。
线程池自动关闭的两个条件:
线程池的引用不可达;
线程池中没有线程。
这里对于条件2解释一下,线程池中没有线程是指线程池中的所有线程都已运行完自动消亡。然而如果我们ThreadPool的核心线程没有超时策略,线程池并不会自动关闭。
所以需要设置:
1//线程池在执行完任务后,经过超时时间,将所有空闲的线程都释放掉,进程池这样进程就可以退出
2pool.allowCoreThreadTimeOut(true);
3
4```java
5
6## 第二种方法
7
8利用Runtime.*getRuntime**()*.addShutdownHook 和guava的方法优雅关闭
9
10```cs
11static {
12 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
13 @Override
14 public void run() {
15 System.out.println("====开始关闭线程池");
16 CommonThreadPool.gracefulShutdown(pool, 10, TimeUnit.SECONDS);
17 System.out.println("====结束关闭线程池");
18 }
19 }));
20 }
21public static boolean gracefulShutdown(ExecutorService threadPool, int shutdownTimeout,
22 TimeUnit timeUnit) {
23 return threadPool == null || MoreExecutors
24 .shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
25 }
误区
不要将线程池线程设置为守护线程,虽然守护线程不会阻止 JVM 退出****,但这样做有问题,如果有还未执行完的任务就会出现异常了,(任务还没执行完就退出)

关注公众号 获取更多精彩内容
