Why java thread is not killed after processing

Viewed 54

I have the following code:

 public static void main(String[] args) throws InterruptedException {

        CompletableFuture.runAsync(() -> {
                try {
                    test1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("1 " + java.lang.Thread.activeCount());
            Thread.sleep(2000);
            System.out.println("3 " + java.lang.Thread.activeCount());
    } 

    private static void test1() throws InterruptedException {
            Thread.sleep(1000);
            System.out.println("2");
    }

It produces the following output:

1 3
2
3 3

Why doesn't the thread finish after its done and I have another thread hanging around? and why is it different from the following:

Thread thread1=new Thread() {
    public void run() {
        try {
            test1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
thread1.start();

that produces:

1 3
2
3 2
1 Answers
Related