1、模拟批量添加数据
@Service
public class AsynService {
@Async
public void batchAdd(){
try {
Thread.sleep(3*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("批量新增数据完成。。。");
}
}
2、添加Controller
@RestController
public class AsynController {
@Autowired
AsynService asynService;
@GetMapping("/hello")
public String hello(){
asynService.batchAdd();
return "success";
}
}
3、添加异步,在启动类添加注解(并在对应service下 使用@Async)
@EnableAsync
@SpringBootApplication
public class SpringBoot11TaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot11TaskApplication.class, args);
}
}