zoukankan      html  css  js  c++  java
  • Android 线程池ThreadPoolExecutor类

    一、ThreadPoolExecutor类使用

    1. 创建线程池源码:

        /**
         * Creates a new {@code ThreadPoolExecutor} with the given initial
         * parameters.
         *
         * @param corePoolSize the number of threads to keep in the pool, even
         *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
         * @param maximumPoolSize the maximum number of threads to allow in the
         *        pool
         * @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.
         * @param unit the time unit for the {@code keepAliveTime} argument
         * @param workQueue the queue to use for holding tasks before they are
         *        executed.  This queue will hold only the {@code Runnable}
         *        tasks submitted by the {@code execute} method.
         * @param threadFactory the factory to use when the executor
         *        creates a new thread
         * @param handler the handler to use when execution is blocked
         *        because the thread bounds and queue capacities are reached
         * @throws IllegalArgumentException if one of the following holds:<br>
         *         {@code corePoolSize < 0}<br>
         *         {@code keepAliveTime < 0}<br>
         *         {@code maximumPoolSize <= 0}<br>
         *         {@code maximumPoolSize < corePoolSize}
         * @throws NullPointerException if {@code workQueue}
         *         or {@code threadFactory} or {@code handler} is null
         */
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory,
                                  RejectedExecutionHandler handler) {
            if (corePoolSize < 0 ||
                maximumPoolSize <= 0 ||
                maximumPoolSize < corePoolSize ||
                keepAliveTime < 0)
                throw new IllegalArgumentException();
            if (workQueue == null || threadFactory == null || handler == null)
                throw new NullPointerException();
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.workQueue = workQueue;
            this.keepAliveTime = unit.toNanos(keepAliveTime);
            this.threadFactory = threadFactory;
            this.handler = handler;
        }

     2. 线程池工作流程

    1. 首先线程池判断基本线程池是否已满。
    2. 线程池判断线程队列是否已满。
    3. 最后线程池判断整个线程池是否已满。
  • 相关阅读:
    Rest_framework-3
    Nginx 之一:编译安装nginx 1.8.1 及配置
    vmware虚拟机克隆CentOS7 出现的网络问题解决办法
    OAuth认证
    5个节点hadoop安装(zookeeper)
    hadoop完整安装
    从vmware模板克隆linux的操作
    ssh 免密码设置失败原因总结
    hadoop-2.6.0.tar.gz的集群搭建(3节点)(不含zookeeper集群安装)
    linux静态IP设置
  • 原文地址:https://www.cnblogs.com/naray/p/8849397.html
Copyright © 2011-2022 走看看