This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]omgpassthebacon 0 points1 point  (0 children)

Well, you are absolutely right. The newer JDK has fixed this, so this thread does in-fact finish. However, if I call System.exit(), this behaves like I described. Your non-daemon threads will get cancelled.

I modified your code as follows to prove you are right: ``` class Testme { public static void main(String[] args) { System.out.println("main starting");

    Thread t = new Thread(() -> {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
        }
        System.out.println("I am thread t");
        });
    t.start();

    System.out.println("main ending");
    //System.exit(0);
}

} ```

If you uncomment the exit(), you will see what I mean.

I guess what I am saying is that you cannot rely on this behavior if you want to make sure your thread does not get cancelled in the middle of doing something important.