[并发]线程池关闭的问题

238 阅读1分钟

背景

来来,先看一段代码,这段代码会发生什么?当然这段代码是有问题的。

public static void main(String[] args) {

        for(int i = 0; i < 100; i++) {
            test(i);
        }

        try {
            Thread.currentThread().join();
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void test(int i) {
        final ExecutorService executorService = Executors.newFixedThreadPool(
                50);

        executorService.submit(new Runnable() {
            @Override
            public void run() {

            }
        },"thread"+i);
    }

分析

结果

会有100个线程池,有5000个线程,可能有人会问为什么?难道线程池的不会根据test方法执行完直接退出吗?

我们看下运行的结果

屏幕快照 2018-06-08 下午8.06.25.png

吃惊吧!

赶紧去看了ThreadPoolExecutor的源码,找到这么一段话

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call 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 allowCoreThreadTimeOut(boolean)

大概的意思: 在线程池中很长时间不再引用且没有剩余线程,线程池将自动关闭。如果您希望确保未引用的池被回收,即使用户忘记调用shutdown,那么必须安排未使用的线程最终死亡,通过设置适当的保持生存时间,使用一个零核心线程或allowCoreThreadTimeOut(boolean)。

总结下: 线程池里面没有核心线程了,线程池会退出,其实这里可以使用Executors.CachedThreadPool 他的核心线程池就是0.当然他会有一个等待时间60L,来标记核心线程池的存活时间

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

我们看下allowCoreThreadTimeOut的方法

allowCoreThreadTimeOut的注释 Sets the policy governing whether core threads may time out and terminate if no tasks arrive within the keep-alive time, being replaced if needed when new tasks arrive. When false, core threads are never terminated due to lack of incoming tasks. When true, the same keep-alive policy applying to non-core threads applies also to core threads. To avoid continual thread replacement, the keep-alive time must be greater than zero when setting true. This method should in general be called before the pool is actively used.

设置控制核心线程是否可以超时和终止的策略, 如果没有任务在保持生存时间内到达, 则在新任务到达时需要替换。 当错误时, 由于缺少传入任务, 核心线程永远不会被终止。 如果为 true, 则适用于非核心线程的相同的保持生存策略也适用于核心线程。为避免连续线程替换, 设置 true 时, 保持生存时间必须大于零。此方法通常应在池被积极使用之前调用。

 public void allowCoreThreadTimeOut(boolean value) {
        if (value && keepAliveTime <= 0)
            throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
        if (value != allowCoreThreadTimeOut) {
            allowCoreThreadTimeOut = value;
            if (value)
              // 关键这里,将线程的worker都中断
                interruptIdleWorkers();
        }
    }

总结

使用完线程需要调用shutdown 或者shutdownNow 线程池要创建为全局变量