多线程实现循环

public void main( List<String> list) {
        List<String> a=new ArrayList<>();
        //自定义线程池
        ExecutorService pool=new ThreadPoolExecutor(5,10,
                1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        final CountDownLatch endGate = new CountDownLatch(list.size());
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        //具体操作
                        
                    } catch (Exception e) {
                   		 e.printStackTrace();
                    }finally {
                        endGate.countDown();
                    }
                }
            };
            pool.execute(run);
            //pool.submit(run);
        }
       
        try {
            endGate.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        pool.shutdown();
        System.out.println("结束!");
        return a;
    }
上一篇:java线程(面试题)


下一篇:一个使用wait与notify的错误案例