@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
任务同时开始,没毛病~~
|