Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 442
huangjs Avatar asked Feb 16 '26 05:02

huangjs


1 Answers

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. Therefore, your for-loop submits the two tasks then ends, and the test function returns before the tasks had the time to complete.

You might try calling ExecutorService::shutdown after the for-loop to let the executor know that all the tasks have been submitted. Then use ExecutorService::awaitTermination to block until the tasks are complete.

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
    }
like image 104
tantalum Avatar answered Feb 17 '26 19:02

tantalum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!