spring-boot 多线程
1 //配置类
2
3 package test;
4
5 import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
6 import org.springframework.context.annotation.ComponentScan;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.scheduling.annotation.AsyncConfigurer;
9 import org.springframework.scheduling.annotation.EnableAsync;
10 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
11
12 import java.util.concurrent.Executor;
13
14 @Configuration
15 @ComponentScan("test")
16 @EnableAsync
17 public class TaskExecutorConfig implements AsyncConfigurer{
18
19 @Override
20 public Executor getAsyncExecutor() {
21 ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
22 taskExecutor.setCorePoolSize(5);
23 taskExecutor.setMaxPoolSize(10);
24 taskExecutor.setQueueCapacity(25);
25 taskExecutor.initialize();
26 return taskExecutor;
27 }
28
29 @Override
30 public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
31 return null;
32 }
33 }
1 //任务执行类
2
3 package test;
4
5 import org.springframework.scheduling.annotation.Async;
6 import org.springframework.stereotype.Service;
7
8 @Service
9 public class AsyncTaskService {
10
11 @Async
12 public void executeAsyncTask(Integer i) {
13 System.out.println("执行异步任务:" + i);
14 }
15 }
1 //运行
2
3 package test;
4
5 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6
7 public class Application {
8
9 public static void main(String[] args) {
10 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
11
12 AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
13 for (int i = 0; i < 20; i++) {
14 asyncTaskService.executeAsyncTask(i);
15 }
16 context.close();
17 }
18 }
spring-boot 多线程的更多相关文章
- Spring boot多线程
1.配置线程配置类 package test; import java.util.concurrent.Executor; import org.springframework.aop.interce ...
- 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用
Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用,Spring Boot 的自动配置是通过 Spring 4.x 的条件注解 @Conditional 来 ...
- Spring Boot 定时任务单线程和多线程
Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...
- spring boot 2X中@Scheduled实现定时任务及多线程配置
使用@Scheduled 可以很容易实现定时任务 spring boot的版本 2.1.6.RELEASE package com.abc.demo.common; import org.slf4j. ...
- Spring Boot 定时+多线程执行
Spring Boot 定时任务有多种实现方式,我在一个微型项目中通过注解方式执行定时任务. 具体执行的任务,通过多线程方式执行,单线程执行需要1小时的任务,多线程下5分钟就完成了. 执行效率提升10 ...
- spring boot 并发请求,其他系统接口,丢失request的header信息【多线程、线程池、@Async 】
场景:一次迭代在灰度环境发版时,测试反馈说我开发的那个功能,查询接口有部分字段数据是空的,后续排查日志,发现日志如下: feign.RetryableException: cannot retry d ...
- (转)spring boot注解 --@EnableAsync 异步调用
原文:http://www.cnblogs.com/azhqiang/p/5609615.html EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Co ...
- spring boot注解 --@EnableAsync 异步调用
EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Component public class Task { @Async public void doT ...
- spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?
spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的? 消息中间件在解决异步处理,模块间解耦和,和高流量场景的削峰,等情况下有着很广泛的应用 . 本文将跟大家一起 ...
- 如何通过Spring Boot配置动态数据源访问多个数据库
之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...
随机推荐
- python高级数据可视化Dash2
k 线国内版 python dash 的应用首页,是用一个 k 线图来做 damo 的,奈何数据源用的 Google,上不去.当然,可以换 yahoo,但是毕竟国内的还是更亲切些. 官方的 demo ...
- P4147 玉蟾宫
P4147 玉蟾宫 给定一个 \(N * M\) 的矩阵 求最大的全为 \(F\) 的子矩阵 Solution 悬线法 限制条件为转移来的和现在的都为 \(F\) Code #include<i ...
- php循环删除文件夹和目录
一定要注意文件夹中的两个目录----> .(当前目录) ..(上级目录) 需要对这两个目录进行过滤, 否则在删除或者遍历是会一直,对于..甚至会向你指定的文件夹的上级目录循环操作,删除重要 ...
- CM记录-操作系统调优
1.避免使用swap分区---将hadoop守护进程的数据交换到磁盘的行为可能会导致操作超时:物理内存(交换)--Swap分区 2.调整内存分配策略---操作系统内核根据vm.overcommit_m ...
- android kotlin Gradle DSL method not found: '1.2.51()'错误,be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
同步的时候遇到这个问题,从log上看是因为gradle的版本不包含kotlin 1.2.51这个method,具体原因我也不是很清楚,大概猜测是kotlin版本的问题,而最新的版本就是1.2.51,所 ...
- IntelliJ IDEA AndroidStudio SVN无法使用
1.Cann't Run Program "SVN" 把勾都去掉,结果没有任何反应.2.重新安装TotoriseSVN,设置Svn.exe路径,主要不要勾选Enable Inter ...
- 14. Spring Boot的 thymleaf公共页抽取
5).CRUD-员工列表实验要求:1).RestfulCRUD:CRUD满足Rest风格:URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作 普通CRUD(uri来区分操作) ...
- 二、主目录 Makefile 分析(1)
2.1 uboot 版本号 uboot 的版本号分三个级别: VERSION:主版本号 PATCHLEVEL:次版本号,为补丁级别 SUBLEVEL:再次版本号 EXTRAVERSION:附加版本信息 ...
- python安装办法
先我们来安装python 1.首先进入网站下载:点击打开链接(或自己输入网址https://www.python.org/downloads/),进入之后,选择64位下载. 2.下载完成后如下图所示 ...
- BSGS算法及其扩展
bsgs算法: 我们在逆元里曾经讲到过如何用殴几里得求一个同余方程的整数解.而\(bsgs\)就是用来求一个指数同余方程的最小整数解的:也就是对于\(a^x\equiv b \mod p\) 我们可以 ...