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 }

本文出自于:hacpai

spring-boot 多线程的更多相关文章

  1. Spring boot多线程

    1.配置线程配置类 package test; import java.util.concurrent.Executor; import org.springframework.aop.interce ...

  2. 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用

    Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用,Spring Boot 的自动配置是通过 Spring 4.x 的条件注解 @Conditional 来 ...

  3. Spring Boot 定时任务单线程和多线程

    Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...

  4. spring boot 2X中@Scheduled实现定时任务及多线程配置

    使用@Scheduled 可以很容易实现定时任务 spring boot的版本 2.1.6.RELEASE package com.abc.demo.common; import org.slf4j. ...

  5. Spring Boot 定时+多线程执行

    Spring Boot 定时任务有多种实现方式,我在一个微型项目中通过注解方式执行定时任务. 具体执行的任务,通过多线程方式执行,单线程执行需要1小时的任务,多线程下5分钟就完成了. 执行效率提升10 ...

  6. spring boot 并发请求,其他系统接口,丢失request的header信息【多线程、线程池、@Async 】

    场景:一次迭代在灰度环境发版时,测试反馈说我开发的那个功能,查询接口有部分字段数据是空的,后续排查日志,发现日志如下: feign.RetryableException: cannot retry d ...

  7. (转)spring boot注解 --@EnableAsync 异步调用

    原文:http://www.cnblogs.com/azhqiang/p/5609615.html EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Co ...

  8. spring boot注解 --@EnableAsync 异步调用

    EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Component public class Task { @Async public void doT ...

  9. spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?

    spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的? 消息中间件在解决异步处理,模块间解耦和,和高流量场景的削峰,等情况下有着很广泛的应用 . 本文将跟大家一起 ...

  10. 如何通过Spring Boot配置动态数据源访问多个数据库

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...

随机推荐

  1. mysql 日志清理

    1.查看binlog日志 show binary logs; 2.删除某个日志文件之前的所有日志文件purge binary logs to 'bin.000106'; 3.再看show binary ...

  2. GO语言的进阶之路-Golang高级数据结构定义

    GO语言的进阶之路-Golang高级数据结构定义 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们之前学习过Golang的基本数据类型,字符串和byte,以及rune也有所了解, ...

  3. 6、Python-元组

    定义 # Python的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号. aTuple = ('et',77,99.9) print(aTuple) 元组的操作 aTu ...

  4. TCP粘包,拆包及解决方法

    在进行Java NIO学习时,发现,如果客户端连续不断的向服务端发送数据包时,服务端接收的数据会出现两个数据包粘在一起的情况,这就是TCP协议中经常会遇到的粘包以及拆包的问题.我们都知道TCP属于传输 ...

  5. ThinkPHP 3.2 vendor()方法的深入研究及Phpqrcode的正确扩展

    ThinkPHP vendor 方法导入第三方类库 第三方类库 第三方类库指除了 ThinkPHP 框架.应用项目类库之外的其他类库,一般由第三方系统或产品提供,如 Smarty.Zend 等系统的类 ...

  6. vue常用UI组件

    Mint UI 项目主页:http://mint-ui.github.io/#!/zh-cn demo:http://elemefe.github.io/mint-ui/#/ github地址:htt ...

  7. Java入门系列(十二)Java反射

    Why--指的是为什么做这件事,也既事物的本质. 反射之中包含了一个“反”的概念,所以要想解释反射就必须先从“正”开始解释,一般而言,当用户使用一个类的时候,应该先知道这个类,而后通过这个类产生实例化 ...

  8. Python分析网页中的<a>标签

    soup = BeautifulSoup(html,"html.parser") html=soup.select("table a") for k in ht ...

  9. 淘淘商城之springmvc前端控制器

    一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  10. pyqt5-布局控件

    在布局中添加控件用addWidght(),添加布局用addLayout() 垂直布局QVBoxLayout 需要导入  from PyQt5.QtWidgets import QVBoxLayout ...