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配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...
随机推荐
- XMind 入门教程
选自:http://www.xmindchina.net/ XMind 是一款非常实用的商业思维导图软件,应用全球最先进的Eclipse RCP 软件架构,全力打造易用.高效的可视化思维软件.对于新手 ...
- git log 查看提交记录,参数:
git log 查看提交记录,参数:-n (n是一个正整数),查看最近n次的提交信息 $ git log -2 查看最近2次的提交历史记录 -- fileName fileName为任意文件名,查看指 ...
- java集合复制和反转
1.for循环方法: 2.System.arraycopy()方法: 3.Arrays.copyOf()方法: 4.Object.clone()方法: + View code /** * @autho ...
- ev的offsetX,pageX,clientX和screenX
event.offsetX.event.offsetY(相对事件发生的具体元素左上角的定位) 鼠标相对于事件源元素(srcElement)的X,Y坐标,只有IE事件有这2个属性,标准事件没有对应的属性 ...
- 设计模式---组件协作模式之观察者模式(Observer)
一:概念 Observer模式的作用是当一个对象的状态发生变化时,能够自动通知其他关联对象,自动刷新对象状态 Observer模式提供给关联对象一种同步通信的手段,使得某个对象与依赖他的其他对象之间保 ...
- javascript 缓动返回顶部案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 数据库sql语句例题(转)
SQL数据库面试题以及答案(50例题) Student(S#,Sname,Sage,Ssex)学生表 S#:学号 Sname:学生姓名 Sage:学生年龄 Ssex:学生性别 Course(C#,Cn ...
- cdqz2017-test11-奇诺之旅(拟阵)
解为基环树森林 证明其具有拟阵的性质: 1.空集独立 2.基环树森林的子集仍然是基环树森林,满足遗传特性 3.对于基环树森林A,B,若|A|<|B| (边数),一定可以找到一条边e∈B,∉A,使 ...
- 一步一步配置 Dell OME 监控 Dell 服务器硬件报警
本文包括以下四个部分: 下载 Dell OME 安装 Dell OME 配置 Dell OME 配置 iDRAC 下载 Dell OME 以Dell PowerEdge R730xd 为例 1.登录 ...
- 获取本机IP地址的小脚本
获取本机私网地址(1个) #!/bin/bash # Author : standby # Date : -- # Description : Get private ip address of lo ...