原文地址:https://www.jianshu.com/p/ec5b8cccd87d

java和spring都提供了线程池的框架

java提供的是Executors;

spring提供的是ThreadPoolTaskExecutor;

一、基本使用

Executors提供了4个线程池,

  1. FixedThreadPool
  2. SingleThreadExecutor
  3. CachedThreadPool
  4. ScheduledThreadPool

FixedThreadPool-固定线程池

public class FixPool {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i =0;i<10;i++){
executorService.execute(()->{
Thread wt = Thread.currentThread(); String format = String.format("当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}

效果如下:

可以看到5个线程在接任务,接完5个任务之后就停止了1秒,完成之后继续接任务;

SingleThreadExecutor-单一线程池,线程数为1的固定线程池

为什么要创造这么一个线程池出来呢?

因为有些时候需要用到线程池的队列任务机制,又不想多线程并发。此时就需要用单一线程池了。

以下两种写法完全一样的效果

ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newFixedThreadPool(1);

CachedThreadPool-缓存线程池,线程数为无穷大的一个线程池

当有空闲线程的时候就让空闲线程去做任务;

当没空闲线程的时候就新建一个线程去任务;

public class CashPool {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool(); for (int i =0;;i++){
executorService.execute(()->{
Thread wt = Thread.currentThread(); String format = String.format("缓存线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
double d = Math.random();
int sleep = (int)(d*5000);
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread.sleep(100);
}
}
}

效果如下:



由于任务耗时不确定,所以线程池会动态根据情况去判断是否创建新的线程;

ScheduledThreadPool-调度线程池,线程会根据一定的时间规律去消化任务

分别有3个

  1. schedule(固定延时才执行任务)
  2. scheduleAtFixedRate(一定的间隔执行一次任务,执行时长不影响间隔时间)
  3. scheduleWithFixedDelay(一定的间隔执行一次任务,从任务执行接触才开始计算,执行时长影响间隔时间)
public class ScheduledPool {
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
String begin = String.format("调度线程池-begin,当前时间:%s", LocalDateTime.now());
System.out.println(begin); // schedule(pool);
scheduleAtFixedRate(pool);
// scheduleWithFixedDelay(pool);
} private static void scheduleAtFixedRate(ScheduledExecutorService pool){
pool.scheduleAtFixedRate(()->{
Thread wt = Thread.currentThread();
String format = String.format("scheduleAtFixedRate-调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
},1,1, TimeUnit.SECONDS);
} private static void scheduleWithFixedDelay(ScheduledExecutorService pool){
pool.scheduleWithFixedDelay(()->{
Thread wt = Thread.currentThread();
String format = String.format("scheduleWithFixedDelay-调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
},1,1, TimeUnit.SECONDS);
}
private static void schedule(ScheduledExecutorService pool){
for (int i =0;i<10;i++){
pool.schedule(()->{
Thread wt = Thread.currentThread();
String format = String.format("调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
},5, TimeUnit.SECONDS);
}
pool.shutdown();
}
}

二、原理讲解

上面介绍的4个线程池工具,都是基于一个类ThreadPoolExecutor

ThreadPoolExecutor有几个重要的参数

  1. corePoolSize,核心线程数
  2. maximumPoolSize,最大线程数
  3. keepAliveTime,线程空闲时间,和4组合使用
  4. unit
  5. workQueue,工作队列,是各个工具的机制策略的核心
  6. threadFactory ,生成线程的工厂,可以代理run方法,还有给thread命名
  7. handler,拒绝策略,当任务太多的时候会执行的方法,框架有4个实现好的策略,可以自己写自己的策略。

对于fixPool线程池,corePoolSize=maximumPoolSize=n,keepAliveTime=0,workQueue=LinkedBlockingQueue,threadFactory和handler都是默认的。

对于cashPool线程池,corePoolSize=0,maximumPoolSize=2^32,keepAliveTime=60s,workQueue=SynchronousQueue,threadFactory和handler都是默认的。

我们先看一看execute方法

其中ctl参数是一个核心参数,保存着线程池的运行状态和线程数量,通过workerCountOf()获取当前的工作线程数量。

execute整个过程分成3个部分。

  1. 当工作线程数少于核心线程数,创建一个工作线程去消化任务。
  2. 当线程池在运行状态而且能把任务放到队列,则接受任务,调用addWorker让线程去消化队列的任务。
  3. 让线程获取消化任务失败,拒绝任务。

核心一 workQueue.offer

对于fixPool,由于workQueue是LinkedBlockingQueue,所以offer方法基本会返回true。

对于cashpool,workQueue是SynchronousQueue,如果没有消费者在take,则会立马返回false,然后立马新建一个线程。

核心二 getTask

每个线程都被包装成worker对象,worker对象会执行自己的runWorker方法,方法在死循环不停得调用getTask方法去消化任务。



getTask里面最核心的是take和poll方法,这个是跟你传入的队列特性有关。

对于spring提供的ThreadPoolTaskExecutor,其实也是对ThreadPoolExecutor的一个封装。

具体看initializeExecutor方法



在执行execute方法的时候,也是执行ThreadPoolExecutor的execute方法。

结论,线程池的核心是ThreadPoolExecutor类,根据传入的workQueue的offer、poll、take方法特性的不同而诞生缓存线程池,固定线程池,调度线程等各种线程策略。

github地址:https://github.com/hd-eujian/threadpool.git

码云地址: https://gitee.com/guoeryyj/threadpool.git

线程池基本使用和ThreadPoolExecutor核心原理讲解的更多相关文章

  1. Java线程池的使用方式,核心运行原理、以及注意事项

    为什么需要线程池 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的系统时间,影响系统效率. 为了解决上面的问题,java中引入了线程池,可以使创 ...

  2. 并发编程系列:Java线程池的使用方式,核心运行原理、以及注意事项

    并发编程系列: 高并发编程系列:4种常用Java线程锁的特点,性能比较.使用场景 线程池的缘由 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的 ...

  3. JDK ThreadPoolExecutor核心原理与实践

    一.内容概括 本文内容主要围绕JDK中的ThreadPoolExecutor展开,首先描述了ThreadPoolExecutor的构造流程以及内部状态管理的机理,随后用大量篇幅深入源码探究了Threa ...

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

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

  5. Java线程池使用和分析(二) - execute()原理

    相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的 ...

  6. 线程池技术之:ThreadPoolExecutor 源码解析

    java中的所说的线程池,一般都是围绕着 ThreadPoolExecutor 来展开的.其他的实现基本都是基于它,或者模仿它的.所以只要理解 ThreadPoolExecutor, 就相当于完全理解 ...

  7. Java并发包源码学习之线程池(一)ThreadPoolExecutor源码分析

    Java中使用线程池技术一般都是使用Executors这个工厂类,它提供了非常简单方法来创建各种类型的线程池: public static ExecutorService newFixedThread ...

  8. 线程池的使用及ThreadPoolExecutor的分析(一)

    说明:本作者是文章的原创作者,转载请注明出处:本文地址:http://www.cnblogs.com/qm-article/p/7821602.html 一.线程池的介绍 在开发中,频繁的创建和销毁一 ...

  9. Executor框架(三)线程池详细介绍与ThreadPoolExecutor

    本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介   java.uitl.c ...

随机推荐

  1. Win10桌面不见了只显示开始菜单该怎么办?

    来源:http://www.w10zj.com/Win10xy/Win10xf_4256.html 在Win10系统中,有用户反应桌面不见了,只显示开始菜单的情况,该怎么办呢?出现这样的情况一般由于桌 ...

  2. STM32之旅3——时钟数

    STM32之旅3--时钟数 STM32F1是M3内核,它的时钟数很庞大,让一个初学者去看,估计会很吃力,和我们入门的8051单片机的时钟不同,这里又倍频.又分频,而且还分成好多个时钟,不同的外设时钟不 ...

  3. PicGo图床与Typora(PicGo+Typora+GitHub的完整设置)

    PicGo图床与Typora(PicGo+Typora+GitHub的完整设置) 如何更方便的用markdown写文章,接下来按照我的步骤来,你一定可以的,这个文章包含了GitHub图床配置.PicG ...

  4. python反序列化学习记录

    pickle与序列化和反序列化 官方文档 模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化. "pickling" 是将 Python 对象及其所拥 ...

  5. React Ref 其实是这样的

    大家好,我是Mokou,好久没有冒泡了,最近一直在看研究算法和数据结构方面的东西,但是似乎很多前端不喜欢看这种东西,而且目前本人算法方面也很挫,就不献丑了. 当然了,最近也开始研究React了,这篇文 ...

  6. day18 Pyhton学习 匿名函数

    匿名函数 别称: lambda表达式 函数,没有名字 def wahaha(n):#wahaha return n**2 print(wahaha.__name__) qqxing = lambda ...

  7. spring boot:给接口增加签名验证(spring boot 2.3.1)

    一,为什么要给接口做签名验证? 1,app客户端在与服务端通信时,通常都是以接口的形式实现, 这种形式的安全方面有可能出现以下问题: 被非法访问(例如:发短信的接口通常会被利用来垃圾短信) 被重复访问 ...

  8. linux(centos8):安装jmeter5.3

    一,jmeter的用途: Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试, 它最初被设计用于Web应用测试,但后来扩展到其他测试领域.  Apach ...

  9. centos8平台mysql日志的按天切分

    一,mysqladmin使用flush-logs的文档: mysql8官网上面针对mysqladmin的文档地址 https://dev.mysql.com/doc/refman/8.0/en/mys ...

  10. deepin vue安装步骤

    deepin安装node.js sudo wget https://nodejs.org/dist/v9.2.0/node-v9.2.0-linux-x64.tar.xz tar xJf node-v ...