zoukankan      html  css  js  c++  java
  • Executor、ExecutorService、ThreadPoolExecutor

    1、Executor

    Executor接口中中只有一个方法

    执行已提交的Runnable任务对象。

    ExecutorService pool1 = Executors.newFixedThreadPool(5);
    ExecutorService pool2 = Executors.newCachedThreadPool();
    ExecutorService pool3 = Executors.newSingleThreadExecutor();
    ExecutorService pool = Executors.newScheduledThreadPool(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,//coreSize
                                  int maximumPoolSize,//maxSize
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,//有界队列和无界队列
                                  ThreadFactory threadFactory,
                                  RejectedExecutionHandler handler) {//拒绝策略或者其他操作,一个对象,可以用new RejectedExecutionHandler(),或者自定义
            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;
        }
  • 相关阅读:
    一顿午饭引发的风波
    用exp无法导出空表解决方法/用exp导出数据时表丢失原因
    程序员的不归路
    IIS权限设置
    超级基础搭建Android开发环境
    业务流程图与数据流程图的比较
    发布asp.net应用程序后,其中导入、导出excel报错的解决方案
    Validation of viewstate MAC failed异常的原因及解决方法
    orarcle11g中服务代表的意思
    最新BIOS设置中英文对照表
  • 原文地址:https://www.cnblogs.com/gudulijia/p/6904875.html
Copyright © 2011-2022 走看看