多线程查询优化

     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 @ 2022-07-20 22:06  别动我的猫  阅读(151)  评论(0编辑  收藏  举报