spring 注解配置定时器

来源:互联网 发布:clojure编程 编辑:程序博客网 时间:2024/04/29 05:02
<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;@Servicepublic 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完成后才会继续


0 0
原创粉丝点击