目录
一、Executor接口的理解
- 一、Executor接口的理解
- 二、Executor接口的类图结构
- 三、Executor接口中常用的方法
- 四、线程池的创建分为两种方式(主要介绍通过ThreadPoolExecutor方式)
- 1、ThreadPoolExecutor类中的构造方法
- 2、 ThreadPoolExecutor类中构造函数的参数解析
- 3、ThreadPoolExecutor类创建线程池示例
- Executor属于java.util.concurrent包下;
- Executor是任务执行机制的核心接口;
- 由类图结构可知:
- ThreadPoolExecutor 继承了AbstractExecutorService接口;
- AbstractExecutorService接口实现了ExecutorService接口;
- ExecutorService继承了Executor接口;
- 因此以下部分主要讲解ThreadPoolExecutor类。
- void execute(Runnable command) 在将来的某个时间执行给定的命令。 该命令可以在一个新线程,一个合并的线程中或在调用线程中执行,由Executor实现。
- 注:通过Executors类的方式创建线程池,参考lz此博文链接https://wwwxz.blog.csdn.net/article/details/117934869
- public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,defaultHandler)
- corePoolSize 核心线程最大数量,通俗点来讲就是,线程池中常驻线程的最大数量
- maximumPoolSize 线程池中运行最大线程数(包括核心线程和非核心线程)
- keepAliveTime线程池中空闲线程(仅适用于非核心线程)所能存活的最长时间
- unit 存活时间单位,与keepAliveTime搭配使用
- workQueue 存放任务的阻塞队列
- handler 线程池饱和策略
- 代码
package com.xz.thread.executor;
import java.util.concurrent.*;
/**
* @description:
* @author: xz
* @create: 2021-06-16 22:16
*/
public class Demo {
public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(3,3,
1L, TimeUnit.MINUTES,new LinkedBlockingDeque());
for(int i=1;i
关注
打赏
热门博文
- Netty——网络编程 NIO(Selector处理accept事件)代码示例
- CompletableFuture异步编排(多任务组合)
- CompletableFuture异步编排(线程串行化代码示例)
- CompletableFuture异步编排(handle最终处理)
- CompletableFuture异步编排(计算完成回调代码示例)
- hutool工具导出excel代码示例
- java 获取音频、视频文件时长代码示例
- PostMan发送请求参数带有路径特殊字符会返回400错误(与URL字符及URL编码值有关)
- Rabbitmq与Erlang安装包下载图解
- idea2021.1版本SpringBoot项目日志的说明及使用