leftso 92 0 2020-09-14 11:48:03

文章位置:左搜> 编程技术> Java编程技术> 正文
线程池创建
    /**
     * 队列用线程
     * @return
     */
    @Bean(name = "queuePool")
    public ThreadPoolExecutor queuePool(){
        ThreadPoolExecutor pool = new ThreadPoolExecutor(8, 16,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(200),
                new ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }
while 循环线程创建
public AsynCompute{
    
    @Resource(name = "queuePool")
    ThreadPoolExecutor executorService;
    
     @PostConstruct
    public void process(){
    
         executorService.execute(()->{
              while (true&&!executorService.isShutdown()){
                  //TODO 具体业务
              } 
         });
    }
}
注意点:executorService.isShutdown()判断线程池是否关闭状态

评论区域

暂无评论,快来抢首发吧!!!