java线程池的关闭

线程池是一个重要的资源,关闭线程池有两种方式:手动和自动。接下来我们一一讲解。

手动关闭

线程池有两个方法 shutdown()/shutdownNow()用来关闭,二者的区别:

  • shutdown() 执行后停止接受新任务,会把队列的任务执行完毕。
  • shutdownNow() 也是停止接受新任务,但会中断所有的任务,将线程池状态变为 stop。

两个方法都会中断线程,用户可自行判断是否需要响应中断。通常,我们可以设置一个shutdownHook来关闭线程池:

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    try {
        executorService.shutdown();
        while(!executorService.awaitTermination(10,TimeUnit.SECONDS)) {}
    } catch(Exception e) {

    }
}));

自动关闭

首先要了解线程池在什么情况下会自动关闭。ThreadPoolExecutor 类(最常用的线程池实现类)的源码注释中有这么一句话:A pool that is no longer referenced in a program and has no remaining threads will be shutdown automatically.没有引用指向且没有剩余线程的线程池将会自动关闭。

那么什么情况下线程池中会没有剩余线程呢?先来看一下 ThreadPoolExecutor 参数最全的构造方法:

/**
 * @param corePoolSize the number of threads to keep in the pool, even
 *           if they are idle, unless {@code allowCoreThreadTimeOut} is set
 *        核心线程数:即使是空闲状态也可以在线程池存活的线程数量,除非           
 *        allowCoreThreadTimeOut 设置为 true。
 * @param keepAliveTime when the number of threads is greater than
 *        the core, this is the maximum time that excess idle threads
 *        will wait for new tasks before terminating.
 *         存活时间:对于超出核心线程数的线程,空闲时间一旦达到存活时间,就会被销毁。
 */
public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                           RejectedExecutionHandler handler) { ... ... }

这里只关心与线程存活状态最紧密相关的两个参数:corePoolSize和keepAliveTime。keepAliveTime参数指定了非核心线程的存活时间,非核心线程的空闲时间一旦达到这个值,就会被销毁,而核心线程则会继续存活,只要有线程存活,线程池也就不会自动关闭。聪明的你一定会想到,如果把corePoolSize设置为0,再给keepAliveTime指定一个值的话,那么线程池在空闲一段时间之后,不就可以自动关闭了吗?没错,这就是线程池自动关闭的第一种情况。

1、自动关闭线程池:核心线程数为 0 并指定线程存活时间

ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 5,
                30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(15));
for (int i = 0; i < 20; i++) {
    executor.execute(() -> {
        // 简单地打印当前线程名称
        System.out.println(Thread.currentThread().getName());
    });
}

执行上述代码,当线程打印结束后等待30s,程序退出(代表了线程池自动关闭了)。其实Executors.newCachedThrteadPool()创建的线程池,coreP00lSize=0且keepAliveTime=60s,所以也可以自动关闭。其源码如下:

public class Executors {
    ... ...
    public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());
    }
    ... ...
}

如果将上面示例的代码corePoolSize改成大于0的数,当线程打印结束后程序一直不会退出。

2、自动关闭线程池:通过 allowCoreThreadTimeOut 控制核心线程存活时间

将核心线程数设置为0虽然可以实现线程池的自动关闭,但也存在一些弊端,根据线程池工作原理,当corePoolSize=0时新到来的任务会永远优先被放入任务队列,然后等待被处理,这显然会影响程序的执行效率。

那你可能要问了,有没有方法来关闭核心线程呢?答案是肯定的,从 JDK 1.6 开始,ThreadPoolExecutor 类新增了一个allowCoreThreadTimeOut字段,这个字段值默认为false,可使用allowCoreThreadTimeOut()方法对其进行设置,如果设置为 true,那么核心线程数也将受keepAliveTime控制,此方法源码如下:

public void allowCoreThreadTimeOut(boolean value) {
    // 核心线程存活时间必须大于0,一旦开启,keepAliveTime 也必须大于0
    if (value && keepAliveTime <= 0)
        throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
    // 将 allowCoreThreadTimeOut 值设为传入的参数值
    if (value != allowCoreThreadTimeOut) {
        allowCoreThreadTimeOut = value;
        // 开启后,清理所有的超时空闲线程,包括核心线程
        if (value)
            interruptIdleWorkers();
    }
}

接下来,把上面例子修改一下,运行:

ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 5,
                30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(15));
executor.allowCoreThreadTimeOut(true);
for (int i = 0; i < 20; i++) {
            executor.execute(() -> {
               // 简单地打印当前线程名称
                System.out.println(Thread.currentThread().getName());
            });
}

 虽然corePoolSize>0,通过设置allowCoreThreadTimeOut=true,当线程打印结束后等待30s,程序正常退出(说明线程池自动关闭了)

3、自动关闭线程池:线程池中的线程设置为守护线程

ThreadPoolExecutor executor2 = new ThreadPoolExecutor(1, 5, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(150),
            new ThreadFactory() { 
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r, r.hashCode()+"");
                    thread.setDaemon(true);//设置成守护线程
                    return thread;
                }
            }
);
for (int i = 0; i < 20; i++) {
            executor2.execute(() -> {
               // 简单地打印当前线程名称
                System.out.println(Thread.currentThread().getName());
            });
}

虽然corePoolSize>0,而且没有设置allowCoreThreadTimeOut,但是在创建线程池时通过ThreadFactory指定了线程为守护线程。当线程打印结束后,无需等待程序正常退出(说明线程池自动关闭了)。当然,这里是在main函数中执行,不存在其他非守护线程哈。

如何优雅的使用和理解线程池 | crossoverJie's Blog

java - Java 线程池会自动关闭吗?_个人文章 - SegmentFault 思否

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值