首页 技术 正文
技术 2022年11月7日
0 收藏 383 点赞 909 浏览 2913 个字

ThreadPoolExecutor:=======这个是java自己实现的线程池执行类,基本上创建线程池都是通过这个类进行的创建。
ThreadPoolTaskExecutor:========这个是springboot基于ThreadPoolExecutor实现的一个线程池执行类。

注意:

在springboot当中,如果没有配置线程池的话,springboot会自动配置一个ThreadPoolTaskExecutor线程池到bean当中,我们调用只需要

@Autowired

ThreadPoolTaskExecutor  threadPoolTaskExecutor;

第一步: 首先在application启动类添加@EnableAsync

@SpringBootApplication
@EnableAsync //首先在application启动类添加@EnableAsync
public class ThreadpoolApplication {
public static void main(String[] args) {
SpringApplication.run(ThreadpoolApplication.class, args);
}
}

第二步:配置线程池,不配置的话使用springboot默认的线程池。

package com.aswatson.csc.task.conf;import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration
@EnableAsync
public class AsyncThreadConfiguration { @Bean("kafkaThreadExecutor")
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);//核心线程数
executor.setMaxPoolSize(20);//最大线程数
executor.setKeepAliveSeconds(60);//空闲线程存活时间
executor.setThreadNamePrefix("kafkaThreadAsync-");
executor.initialize();
return executor;
}}

第三步:测试1:在需要异步执行的方法上加上@Async注解。

@Service
public class AsyncTest {
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Async
public void hello(String name){
customerEventLogMapper.insert(customerEventLog);
logger.info("异步线程启动 started."+name);
}
}

第四步:测试2:使用注入的模式:

package com.example.apidemo.completableFutrue;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;@Service
public class AsyncService { @Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor; public void addEventLog(String buId, String status){
CustomerEventLogPO customerEventLog = new CustomerEventLogPO();
customerEventLog.setUuid(uuid);
customerEventLog.setStatus(status);
customerEventLog.setCreated(LocalDateTime.now());
customerEventLogMapper.insert(customerEventLog); threadPoolTaskExecutor.submit(new Thread(()->{
customerEventLogMapper.insert(customerEventLog);
})); //submit有返回值 threadPoolTaskExecutor.execute(new Thread(()->{
customerEventLogMapper.insert(customerEventLog);
})); //execute无返回值
}}

注意: 如果配置多个线程池,该如何指定线程池呢?

方式1: @Resources(“kafkaThreadExecutor”)。

方式2: 如果有多个线程池,但是在@Async注解里面没有指定的话,会默认加载第一个配置的线程池。

======================================================================================================================================================================

// @Bean()的拒绝策略:
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

拒绝策略:如果(总任务数 – 核心线程数 – 任务队列数)-(最大线程数 – 核心线程数)> 0 的话,则会出现线程拒绝。举例:( 12 – 5 – 2 ) – ( 8 – 5 ) > 0,会出现线程拒绝。线程拒绝又分为 4 种策略,分别为:

    • CallerRunsPolicy():交由调用方线程运行,比如 main 线程。
    • AbortPolicy():直接抛出异常。
    • DiscardPolicy():直接丢弃。
    • DiscardOldestPolicy():丢弃队列中最老的任务。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848