<pre name="code" class="html"><!-- 自动包扫描配置 -->
<context:component-scan base-package="com.xx" />
首先配置加上任务注解驱动配置
<task:annotation-driven />
package com.wjxjt.ddt.message.service;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class TestScheduleService {
private AtomicLong atomicLong = new AtomicLong(0);
@Scheduled(cron="*/5 * * * * ?")
public void job(){
System.out.println("定时器"+atomicLong.incrementAndGet());
if(atomicLong.longValue()==5l){
System.out.println("开始阻塞");
try {
Thread.sleep(9000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
以上代码spring定时器每隔5秒钟执行,但是如果方法里面死锁或者阻塞了,那么下次5秒钟是不会再执行了,必须等上一次任务执行完成才会继续,下面是运行结果
定时器1
定时器2
定时器3
定时器4
定时器5
开始阻塞 //再这里就会阻塞,下一次五秒并没有开始任务,必须等sleep完成后才会继续