多线程查询优化

复制代码
     Semaphore semaphore = new Semaphore(nThread);
        //创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
                                        nThread,
                                        0L,
                                        TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<>(30),
                                        new ThreadFactoryBuilder().setNameFormat("线程名称").build(),
                                        new ThreadPoolExecutor.AbortPolicy());    
                                        
        for (T number : list) {
            try {
                semaphore.acquire();
                executor.execute(() -> {
                //此处可以放入待处理的业务
                System.out.println("number:" + number);
                semaphore.release();
                });
            } catch (InterruptedException e) {

            }
        }
        executor.shutdown();
复制代码

 

posted @   别动我的猫  阅读(157)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示