P
P
parkito2019-06-12 03:21:20
Java
parkito, 2019-06-12 03:21:20

Why doesn't ExecutorService require shutdown in junit?

Good afternoon.
If using ExecutorService in main method, then shutdown must be used to terminate the program

public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(() -> Utils.sleepSeconds(1));

        System.out.println("Done");
        executorService.shutdown();
    }

However, this is not required in a Junit test. The test ends without shutdown.
@org.junit.jupiter.api.Test
    public void test() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(() -> Utils.sleepSeconds(1));

        System.out.println("Done");
    }

I don't quite understand why this is happening. What does Junit do to avoid the need for shutdown?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question