springboot使用多线程

一、使用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;
    }

}

上一篇:JUC练习10——线程池


下一篇:多线程并发测试,用了都说好