美文网首页
[并发]线程池关闭的问题

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

作者: 程序员驿站 | 来源:发表于2018-06-08 20:12 被阅读139次

背景

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

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
线程池要创建为全局变量

相关文章

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

    背景 来来,先看一段代码,这段代码会发生什么?当然这段代码是有问题的。 分析 结果 会有100个线程池,有5000...

  • Java多线程--JDK并发包(2)

    Java多线程--JDK并发包(2) 线程池 在使用线程池后,创建线程变成了从线程池里获得空闲线程,关闭线程变成了...

  • Java并发 - 并发编程实战

    Java并发 - 线程Java并发 - 线程池Java并发 - Executor/ExecutorService/...

  • Java线程池的关闭

    一 java线程池关闭方法 1.1 shutdown void shutdown() 此方法关闭线程池不是直接关闭...

  • 线程池的原理和AsyncTask

    线程池 1.什么是线程池?为什么要用线程池? Java中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执...

  • Java并发系列:线程池ThreadPoolExecutor参数

    上篇回顾 上篇我们讲了,线程池的创建、提交任务到线程池、关闭线程池。基本使用我们已经没有多大问题了。上节我们创建线...

  • java-threadpool

    线程池的好处 利用线程池管理多线程可以控制最大并发的问题,同时可以只能加服务器资源的利用率 线程池的简单使用 1,...

  • 面试题系列:并发编程之线程池及队列

    并发编程之线程池及队列 问题: 线程池作用,主要实现类,并说出实现类场景以及区别。 ThreadPoolExecu...

  • 多任务并发时,该怎样判断线程池中的任务都已经执行完毕?

    开发中常常遇到需要用线程池来解决多任务并发的问题,在使用JUC下的线程池执行并发任务之后,往往很难判断任务是不是都...

  • 线程

    Java 并发编程:线程池的使用 Java 并发编程:线程池的使用java 多线程核心技术梳理 (附源码) 本文对...

网友评论

      本文标题:[并发]线程池关闭的问题

      本文链接:https://www.haomeiwen.com/subject/toaasftx.html