[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]spring使用@Scheduled注解定时任务时,注意配置线程池

上一篇:[备忘]java瞎写的,几个字母全排列
下一篇:[备忘]谷歌浏览器报错:ERR_INSUFFICIENT_RESOURCES

添加日期:2023/6/21 11:31:15 快速返回   返回列表 阅读218次


@Scheduled(cron = "0 0/1 * * * ?")
public void execute1111() {
    System.out.println("111 begin");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("111 end");
}
@Scheduled(cron = "0 0/1 * * * ?")
public void execute2222() {
    System.out.println("222 begin");
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("222 end");
}
@Scheduled(cron = "0 0/1 * * * ?")
public void execute3333() {
    System.out.println("333 begin");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("333 end");
}


如上,运行结果可能如下:
333 begin
333 end
111 begin
111 end
222 begin
222 end
3个任务是串行的,因为默认线程池size是1,就会导致时间到了,任务还没有执行。

解决办法:
spring配置文件里加上:
<task:scheduler id="myScheduler" pool-size="10"/>
即可

输入结果可能如下:
222 begin
111 begin
333 begin
111 end
222 end
333 end

任务同时开始,没毛病~~
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved