如何使用线程池读取多个文件?

时间:2019-04-12 13:43:34

标签: java multithreading file java-8

我想使用线程池读取多个文件,但是失败了。

@Test
public void test2() throws IOException {
    String dir = "/tmp/acc2tid2999928854413665054";
    int[] shardIds = new int[]{1, 2};
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    for (int id : shardIds) {
        executorService.submit(() -> {
            try {
                System.out.println(Files.readAllLines(Paths.get(dir, String.valueOf(id)), Charset.forName("UTF-8")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

上面是我写的一个简单示例。它达不到我的目的。

System.out.println(Files.readAllLines(
        Paths.get(dir, String.valueOf(id)), Charset.forName("UTF-8")));

此行将不会运行,也没有警告。我不知道为什么?

1 个答案:

答案 0 :(得分:7)

您正在提交要执行的任务,然后在等待任务完成之前结束测试。 ExecutorService::submit将提交将来要执行的任务并立即返回。因此,您的for循环提交了两个任务,然后结束,并且测试功能在任务有时间完成之前返回了。

您可以尝试在for循环之后调用ExecutorService::shutdown,以使执行者知道所有任务都已提交。然后使用ExecutorService::awaitTermination进行阻止,直到任务完成。

例如:


    @Test
    public void test2() throws IOException {
        String dir = "/tmp/acc2tid2999928854413665054";
        int[] shardIds = new int[]{1, 2};
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int id : shardIds) {
            executorService.submit(
                    () -> {
                        try {
                            System.out.println(Files.readAllLines(Paths.get(dir, String.valueOf(id)), Charset.forName("UTF-8")));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        }
        executorService.shutdown();
        executorService.awaitTermination(60, TimeUnit.SECONDS); //Wait up to 1 minute for the tasks to complete
    }
相关问题