1、配置异步线程池

  1. import java.util.concurrent.Executor;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.scheduling.annotation.EnableAsync;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. @Configuration
  8. @EnableAsync//开启异步事件的支持
  9. public class AsyncConfig {
  10.  
  11. /*
  12. * 此处成员变量应该使用@Value从配置中读取
  13. */
  14. private int corePoolSize = 10;
  15. private int maxPoolSize = 200;
  16. private int queueCapacity = 10;
  17.  
  18. @Bean
  19. public Executor taskExecutor() {
  20. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  21. executor.setCorePoolSize(corePoolSize);
  22. executor.setMaxPoolSize(maxPoolSize);
  23. executor.setQueueCapacity(queueCapacity);
  24. executor.initialize();
  25. return executor;
  26. }
  27.  
  28. }

2、定时任务类

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.scheduling.annotation.Async;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @EnableScheduling
  9. public class ScheduledService {
  10.  
  11. private Logger logger = LoggerFactory.getLogger(ScheduledService.class);
  12.  
  13. @Scheduled(cron = "0/5 * * * * *")
  14. @Async
  15. public void scheduled() {
  16. logger.info("=====>>>>>使用cron {}", System.currentTimeMillis());
  17. }
  18.  
  19. @Scheduled(fixedRate = 5000)
  20. @Async
  21. public void scheduled1() {
  22. logger.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
  23. }
  24.  
  25. @Scheduled(fixedDelay = 5000)
  26. @Async
  27. public void scheduled2() {
  28. logger.info("=====>>>>>fixedDelay{}", System.currentTimeMillis());
  29. }
  30. }

  

springboot-定时任务-多线程的更多相关文章

  1. 【使用篇二】SpringBoot定时任务Scheduled(14)

    在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...

  2. SpringBoot定时任务 - 开箱即用分布式任务框架xxl-job

    除了前文介绍的ElasticJob,xxl-job在很多中小公司有着应用(虽然其代码和设计等质量并不太高,License不够开放,有着个人主义色彩,但是其具体开箱使用的便捷性和功能相对完善性,这是中小 ...

  3. springboot 定时任务部署至linux服务器上后会执行两次问题

    springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...

  4. springboot下多线程开发注意事项

    基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势.所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项. 第一步我们 ...

  5. Spring boot(三) springboot 定时任务

    这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...

  6. springboot定时任务之旅

    springboot定时任务 假设场景:单体应用的定时任务,假设我们已经有了一个搭建好的springboot应用,但是需要添加一个定时执行的部分(比如笔者遇到的是定时去请求一个接口数据来更新某个表), ...

  7. SpringBoot定时任务@Scheduled

    SpringBoot定时任务主要由两个注解完成. @Scheduled加在方法上面. @EnableScheduling加在类上面.可以是Application类,也可以是@Component类,还可 ...

  8. 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解

    笔记 1.SpringBoot定时任务schedule讲解     简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类            ...

  9. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  10. SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)

    最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...

随机推荐

  1. 陈硕muduo

    https://github.com/chenshuo/muduo muduo 阅读 https://www.cnblogs.com/qbits/p/11101678.html

  2. 1121 Django基本

    目录 Django前戏 一.课程导读 1.web应用 2.c/s b/s 架构 3.Python Web框架 二.原生socket服务 三.http协议 什么是http协议 四大特性 http工作原理 ...

  3. PHP mmysqli_affected_rows(connection);函数

    mysqli_affected_rows(); 函数返回前一次 MySQL 操作所影响的记录行数. mysqli_affected_rows(connection); connection 必需.规定 ...

  4. MySQL 几种性能测试的工具使用

    近期由于要比较mysql及其分支mariadb, percona的性能,了解了几个这方面的工具,包括:mysqlslap sysbench tpcc-mysql,做一个整理,备忘,分享 1.mysql ...

  5. sqlserver中产生随机字符,随机数

    SELECT REPLACE(NEWID(),'-','') select replicate(char(cast(rand()*1000 as int)%26+97) +char(cast(rand ...

  6. netstat 基本用法

    Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字.如果你想确认系统上的 ...

  7. leetcode解题报告(7):Valid Parentheses

    描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  8. 2019 Multi-University Training Contest 10

    目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...

  9. P4781 拉格朗日插值

    #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #def ...

  10. iOS Jenkins 自动化打包构建

    前言 在测试app项目过程中,通常都是需要开发打测试包给到测试,但是无论是iOS还是Android的打包过程都是相当漫长的,频繁的回归测试需要频繁的打包,对于开发同学影响还是蛮大的.因此在这种情况下, ...