一:在主程序开启异步注解功能@EnableAsync
@EnableAsync//开启异步注解功能
@SpringBootApplication
public class AsynTaskApplication {
public static void main(String[] args) {
SpringApplication.run(AsynTaskApplication.class, args);
}
}
二:标明异步类@Asyns
@Service
@Async//表注该类为异步类
public class AsynService {
public void hello(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理。。。。。");
}
}
**
二定时任务
**
1、在主程序中加入@EnableScheduling//开启定时功能
2、在要被定时执行的方法中加入@Scheduled(cron = “0/2 * * * * ?”)
@Service
public class MyScheduled {
@Scheduled(cron = "0/2 * * * * ?")
public void an(){
System.out.println("定时被执行");
}
}