启动页
package com.yunda.demo2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling //开启定时任务
@EnableAsync //开启异步
@SpringBootApplication
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
定时任务注解
package com.yunda.demo2.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class TaskC {
//秒 ,分 ,时 ,日 ,月 ,周
@Scheduled(cron = "0 * * * * MON-SAT") //定时任务注解,整分钟
@Scheduled(cron = "0,1,2,3 * * * * MON-SAT") //定时任务注解,整分钟 逗号是枚举
@Scheduled(cron = "0-3 * * * * MON-SAT") //定时任务注解,整分钟 -代表区间
@Scheduled(cron = "0/3 * * * * MON-SAT") //定时任务注解,整分钟 /代表步长 ,每3秒执行一次
public void hello(){
System.out.println("我啊是你的");
};
}
异步
package com.yunda.demo2.task;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class Taskwang {
@Async //异步注解
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("王胜波ceLLL");
}
}

网友评论