@EnableAsync @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@Service public class CategoryService { @Async public void add(Category category) { // add category } }
@RestController @RequestMapping("/category") public class CategoryController { @Autowired private CategoryService categoryService; @PostMapping("/add") public void add(@RequestBody category) { categoryService.add(category); } }
executor.submit(task);
protected void doExecute(Runnable task) { Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task)); thread.start(); }
@Async在开启异步功能时不要忘记定义线程池。