zoukankan      html  css  js  c++  java
  • java 线程池参数

    创建线程池的构造函数如下:

    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.acc = System.getSecurityManager() == null ?
                    null :
                    AccessController.getContext();
            this.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.workQueue = workQueue;
            this.keepAliveTime = unit.toNanos(keepAliveTime);
            this.threadFactory = threadFactory;
            this.handler = handler;
        }
     
    corePoolSize        核心线程数,线程池中最小线程数,即使线程空闲,也不会被销毁;任务首先提交到核心线程中执行。(初始化时不创建线程,提交任务时创建线程)(如果设置allowCoreThreadTimeOut为true,核心线程也会被销毁,使用较少)
    maximumPoolSize 线程池中允许创建的最大线程数。
    keepAliveTime 当线程数大于corePoolSize时,线程最多等待keepAliveTime时间后,将被销毁。
    unit keepAliveTime参数的时间单位,一般为毫秒。
    workQueue 提交任务的缓存队列。
    threadFactory 创建线程的工厂。
    handler 当corePoolSize已满,缓存队列已满,maximumPoolSize已满时,又发生任务提交时的处理器。
     
    提交任务时:
    如果线程数小于核心线程数(corePoolSize)(或者核心线程有空闲),则新创建线程(或者使用空闲线程)执行任务;
    如果线程数等于核心线程数,则将任务缓存到队列(workQueue);
    如果缓存队列已满,则继续创建线程执行任务,直到达到最大线程数;
    如果线程数达到最大线程数(maximumPoolSize),再提交任务,将使用handler来处理无法处理的任务。

  • 相关阅读:
    OpenCV图像处理之 Mat 介绍
    linux 更改网卡名称 eth0
    【git】git常用命令
    【JS】函数提升变量提升以及函数声明和函数表达式的区别
    【VUE】vue中遍历数组和对象
    加密盐的意义和用途
    sql server2005版本中,len函数计算了字符串末尾的空格
    ES之一:API使用及常用概念
    flink (一)
    ClassLoader详解 (JDK9以前)
  • 原文地址:https://www.cnblogs.com/silenceshining/p/15631015.html
Copyright © 2011-2022 走看看