The fixed-length multi-threaded thread

public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        ExecutorCompletionService<String> service = new ExecutorCompletionService(executor);
        List<Future<String>> result = new ArrayList<Future<String>>();
        for (int i = 0; i < 100; i++) {
            result.add( service.submit(() -> {
                return gety();
            }));
        }
        for (Future<String> f : result) {
            System.out.println(f.get());
        }
        executor.shutdown();
    }



    public static String  gety() throws InterruptedException {
        System.out.println("开始执行");
        Thread.sleep(2000l);
        System.out.println("结束执行");
        return "ok";
    }

 

Guess you like

Origin www.cnblogs.com/yy123/p/10935293.html