private static ExecutorService executors = new ThreadPoolExecutor(5, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), new ThreadFactoryBuilder().setNameFormat("抓数据线程-%d").build());
public static void main(String[] args) throws ExecutionException, InterruptedException {
        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executors);
        // 构造返回结果
        List<ListenableFuture<String>> futures = Lists.newArrayList();
        IntStream.rangeClosed(1, 10).boxed().forEach((page) -> {
            ListenableFuture<String> listenableFuture = listeningExecutorService.submit(() -> getPageData(page));
            futures.add(listenableFuture);
        });
        List<String> strings = com.google.common.util.concurrent.Futures.successfulAsList(futures).get();
        strings.forEach(System.out::println);
    }

 

posted on 2023-10-17 14:54  十七年蝉  阅读(81)  评论(0编辑  收藏  举报