您现在的位置是: 首页 > 后端开发 SpringBoot 定时任务 多线程
SpringBoot 定时任务 多线程
2020-08-03 【后端开发】 1028人已围观 1971次浏览
简介SpringBoot 定时任务 多线程
SpringBoot 中如果存在很多个定时任务,在单线程的情况下,有时候就会出现一个耗时很长的定时任务会一直占据资源,导致其他定时任务不执行或执行延时,这时候就可以使用多线程定时任务来解决这个问题。
首先定义一个定时任务线程池(原有的定时任务配置不用修改)
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;
/**
* 定时任务线程池
*/
@Configuration
// 所有的定时任务都放在一个线程池中,定时任务启动时使用不同的线程。
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 设定一个长度10的定时任务线程池(这里可以根据自己项目中的定时任务数量来配置)
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
}
}
之后再次执行定时任务,就会发现是通过不同的线程来操作的,并且耗时很长的定时任务也不会影响到其他定时任务
很赞哦! (0)
相关文章
- start.spring.io访问太慢,构建SpringBoot项目失败
- Nestedset 出现 Node must exists. 错误解决方案
- PHP 在执行 composer install 时出现提示 PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>
- SqlServer 查询包含指定字段的存储过程
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- Laravel Dingo/api 出现 The version given was unknown or has no registered routes.报错
- 删除Git仓库所有历史提交记录,成为一个干净的仓库
- SqlServer字符串处理
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- mac idea spring boot 启动慢
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- Debian apt 使用国内镜像
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- nuxt 项目完整部署方案
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
站长推荐
猜你喜欢
- SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
- Debian/Ubuntu无netstat命令解决方案
- docker mysql 导入 SQL 脚本
- Redis 提示 MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk 解决方案
- Vue 路由跳转错误 NavigationDuplicated: Avoided redundant navigation to current location
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- Linux 修改默认的 ssh 22 端口
- 删除Git仓库所有历史提交记录,成为一个干净的仓库
- ThinkPHP 解决跨域访问问题
- idea去掉Smart commands execution提示