简体   繁体   English

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

[英]How to read multiple files using thread pool?

I want to read multiple files using a thread pool, but I failed. 我想使用线程池读取多个文件,但失败了。

@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();
            }
        });
    }
}

Above is a simple example I wrote. 上面是我写的一个简单示例。 It cannot reach my purpose. 它达不到我的目的。

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

This line will not run and there were no warnings. 该行将不会运行,也没有警告。 I don't know why? 不知道为什么

You are submitting tasks to be executed then ending the test before waiting for the tasks to complete. 您正在提交要执行的任务,然后在等待任务完成之前结束测试。 ExecutorService::submit will submit the task to be executed in the future and return immediately. ExecutorService::submit将提交要在将来执行的任务,并立即返回。 Therefore, your for-loop submits the two tasks then ends, and the test function returns before the tasks had the time to complete. 因此,您的for循环提交了两个任务,然后结束,并且在任务有时间完成之前,测试函数将返回。

You might try calling ExecutorService::shutdown after the for-loop to let the executor know that all the tasks have been submitted. 您可以尝试在for循环之后调用ExecutorService::shutdown ,以使执行程序知道所有任务均已提交。 Then use ExecutorService::awaitTermination to block until the tasks are complete. 然后使用ExecutorService::awaitTermination进行阻止,直到任务完成。

For example: 例如:


    @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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM