一、使用ExecutorService
1、配置线程池数量
@Configuration public class ThreadPoolConfig { @Bean public ExecutorService getThreadPool(){ return Executors.newFixedThreadPool(8); } }
2、service 使用方式。
@Service public class UserServiceImpl implements IUserService { @Autowired private ExecutorService executorService; @Override public User query() { executorService.execute(new Runnable() { public void run() { System.out.println("Asynchronous task"); } }); User user = new User(); return user; } }