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. non-member function cannot have cv-qualifier

    Q: non-member function unsigned int abs(const T&) cannot have cv-qualifier. template<typename ...

  2. python中的os模块

    os模块 os模块的作用: os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小…… 另 ...

  3. Database in BioInformation

    很多数据库都可以通过下面的网站下载:http://annovar.openbioinformatics.org/en/latest/user-guide/download/ 一.NHLBI-ESP(E ...

  4. CentOS Linux release 7.3源码安装zabbix

    CentOS Linux release 7.3安装zabbix 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 前言: 我去年用用centos6的环境搭建了一下 zabbix3.0 ...

  5. JavaEE学习总结(十六)— Servlet

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  6. filebeat多个key

    filebeat.prospectors:- type: log paths: - D:\logs\iis\W3SVC2\*.log exclude_lines: ['^#'] multiline: ...

  7. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  8. android ContentObserver内容观察者基本使用

    package com.example.observertest; import android.content.ContentResolver; import android.database.Co ...

  9. wcf事务

    wcf服务 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serial ...

  10. Linux之Ubuntu下如何查看已安装的软件/库文件【摘抄】

    本文属于实用性质,且属于摘抄别处,出自:[Ubuntu 下如何查看已安装的软件](http://blog.csdn.net/m1205979825/article/details/40855583) ...