Java多线程之ThreadPoolExecutor

线程池的作用就是使用更少的线程, 来执行更多的任务, 以达到线程的充分利用. 从ThreadPoolExecutor类的构造方法学习:

/** * Creates a new {@code ThreadPoolExecutor} with the given initial * parameters and default rejected execution handler. * * @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 * @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} is null */public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

ThreadPoolExecutor的具体执行流程有四步:

(1) 当目前执行线程的总数小于核心线程大小corePoolSize时,  提交了任务, 会立马新建一个线程来执行提交的任务.

(2) 当目前执行线程的总数达到了核心线程大小corePoolSize时, 就会将当前提交的任务存到缓存队列中

(3) 当缓存队列满了之后, 如果当前线程的总数小于maximumPoolSize时, 此时就会新建线程进行救急处理, 提交的任务会立马执行.

(4) 当线程数达到maximumPoolSize后, 就会采用拒绝策略拒绝任务

ThreadPoolExecutor的学习的更多相关文章

  1. ThreadPoolExecutor简单学习

    Executors和ThreadPoolExecutor两者的区别和联系 jdk中文文档 https://blog.fondme.cn/apidoc/jdk-1.8-google/ 还可以的两个博客 ...

  2. 线程池ThreadPoolExecutor的学习

    我们知道,ExecutorService是一个抽象出线程池的一个接口,然后我们在使用线程池的时候,用的是Executors工具类中的一系列newCachedThreadPool() 等类似的方法,这些 ...

  3. ThreadPoolExecutor 学习笔记

    线程池的奥义 在开发程序的过程中,很多时候我们会遇到遇到批量执行任务的场景,当各个具体任务之间互相独立并不依赖其他任务的时候,我们会考虑使用并发的方式,将各个任务分散到不同的线程中进行执行来提高任务的 ...

  4. 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理

    1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...

  5. 手写线程池,对照学习ThreadPoolExecutor线程池实现原理!

    作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...

  6. Java并发包源码学习系列:线程池ThreadPoolExecutor源码解析

    目录 ThreadPoolExecutor概述 线程池解决的优点 线程池处理流程 创建线程池 重要常量及字段 线程池的五种状态及转换 ThreadPoolExecutor构造参数及参数意义 Work类 ...

  7. 手写一个线程池,带你学习ThreadPoolExecutor线程池实现原理

    摘要:从手写线程池开始,逐步的分析这些代码在Java的线程池中是如何实现的. 本文分享自华为云社区<手写线程池,对照学习ThreadPoolExecutor线程池实现原理!>,作者:小傅哥 ...

  8. ThreadPoolExecutor源码学习(1)-- 主要思路

    ThreadPoolExecutor是JDK自带的并发包对于线程池的实现,从JDK1.5开始,直至我所阅读的1.6与1.7的并发包代码,从代码注释上看,均出自Doug Lea之手,从代码上看JDK1. ...

  9. ThreadPoolExecutor源码学习(2)-- 在thrift中的应用

    thrift作为一个从底到上除去业务逻辑代码,可以生成多种语言客户端以及服务器代码,涵盖了网络,IO,进程,线程管理的框架,着实庞大,不过它层次清晰,4层每层解决不同的问题,可以按需取用,相当方便. ...

随机推荐

  1. PostgreSQL Type的创建与Type在函数中的使用

    postgres=# create type complex as(postgres(# r double precision,postgres(# i double precisionpostgre ...

  2. javascript 内置对象

    什么是对象 javascript中的所有事物都是对象:字符串  数组  数值  函数... 每个对象都带有属性和方法 javascript允许自定义对象      自定义对象: 定义并创建对象实例 使 ...

  3. Apache与Nginx虚拟主机设置(多域名和多端口的区别)

    为了方便管理虚拟主机,应该尽量少修改主配置文件http.conf或者nginx.conf,大部分修改变更都在虚拟主机片配置文件httpd- vhost.conf或者vhost.conf中完成,这样有利 ...

  4. 禁用cookie后session是如何设置的

    我们都知道当在session 会话有基于cookie和基于url两种传递SESSIONID的方法.为了实现客户端禁止cookie发送的情况也不影响客户登陆网站,可以设置 php.ini中 sessio ...

  5. yii框架中邮箱激活(数字签名)

    控制器: //发送邮箱,激活账号   public function actionEmail()   {        $email=Yii::$app->request->get('em ...

  6. 转:装完Centos7提示Initial setup of CentOS Linux 7 (core)

    在用U盘装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License inf ...

  7. explode and implode

    [PHP源码阅读]explode和implode函数   explode和implode函数主要用作字符串和数组间转换的操作,比如获取一段参数后根据某个字符分割字符串,或者将一个数组的结果使用一个字符 ...

  8. paper 91:边缘检测近期最新进展的讨论

    VALSE QQ群对边缘检测近期最新进展的讨论,内容整理如下: 1)推荐一篇deep learning的文章,该文章大幅度提高了edge detection的精度,在bsds上,将edge detec ...

  9. -XX:+PrintGCTimeStamps 打印CG发生的时间戳

    -XX:+PrintGCTimeStamps –打印CG发生的时间戳   –[GC[DefNew: 4416K->0K(4928K), 0.0001897 secs] 4790K->374 ...

  10. 夺命雷公狗---node.js---10之POST的接收

    首先我们在项目下创建一个表单,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta ...