- 简单来说就是开启多线程
代码实现
- 主启动程序添加
@EnableAsync
@SpringBootApplication
@EnableAsync
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
- service方法添加
@Async
@Service
public class AsyncService {
@Async
public void hello(){
System.out.println("处理数据中...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理完毕。");
}
}
Comments | 0 条评论