0
public class SousThread implements Runnable {
    private int i;

    public SousThread() {
        this.i = 0;
    }

    @Override
    public void run() {
        while(true) {
            System.out.println("I'm sousthread " + i++);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class TestThread implements Runnable {

    @Override
    public void run() {
        System.out.println("I'm thread");
        SousThread st = new SousThread();
        Thread td = new Thread(st);
        td.start();
        System.out.println("thread finished");
    }

    public static void main(String[] args) {
        TestThread tt = new TestThread();
        Thread td = new Thread(tt);
        td.start();
        System.out.println("main finished");
    }
}

I'm trying to create thread SousThread in thread TestThread. SousThread is infinite.

To my surprise, I get the result below:

I'm thread
main finished
thread finished
I'm sousthread 0
I'm sousthread 1
I'm sousthread 2
I'm sousthread 3
I'm sousthread 4
I'm sousthread 5
...
...

Does it mean that the method main has finished whereas the thread hasn't finished yet? Is this possible? Is this safe? If not, what is the better way to do?

UPDATE

Because the code like this doesn't work in C++. So I just want to know if the code works without any problem or any risk in Java.

8
  • 1
    Better way to do what? "Is this possible?" Well, clearly. "Is this safe?" Safe in what way? Commented Mar 24, 2017 at 21:47
  • @Kayaman main has finished, but the thread which belongs to main hasn't finished. Doesn't it mean something to you? Commented Mar 24, 2017 at 21:49
  • Not particularly, but that's probably because I understand how threading works in Java. Commented Mar 24, 2017 at 21:50
  • @Kayaman So we can do like this or not? Commented Mar 24, 2017 at 21:50
  • You just did it, didn't you? Commented Mar 24, 2017 at 21:51

3 Answers 3

1

This is well documented and completely normal:

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

In your case, while the main thread has terminated (implicitly by main() returning), the other threads are still alive. Unless you call System.exit() somewhere or set the other threads to be daemon threads, the JVM will continue running until all of your threads have joined.

If you want all threads to terminate before the main thread exists, you need to Thread.join() them. Note that Runtime.exit() terminates threads quite harshly. Threads are not interrupted nor are their finalizers called, so this is really not safe.

Sign up to request clarification or add additional context in comments.

5 Comments

So my code hasn't any risk or any problem (except that JVM will take care of that infinite thread )?
@Yuhui Well, SousThread does not have reliable way of terminating at all, so I'd not say that. But that problem is not caused by returning from main().
It's important to realize that daemon status of the threads is gotten from parent that spawned it. Since main is a non-daemon thread then the TestThread thread and the SousThread thread are also both non-daemon. You should use thread.setDaemon(...) before calling start() if you was specific behavior.
@Gray ... which still leaves the problems that daemon threads don't go down "nicely" on JVM exit, so I'd say that's best avoided, anyway.
No, there are good reasons to have daemon threads. See my answer here: stackoverflow.com/a/10298353/179850
1

What you did in your code is explained below:

  1. main thread was initiated first by JVM
  2. TestThread was started by main thread
  3. SousThread was started by TestThread

Does it mean that the method main has finished whereas the thread hasn't finished yet? Is this possible?

Yes, each thread runs parallely and main thread finished first and then the TestThread finished, but the SousThread thread still runs (because of infinite while loop).

Also, one more point that you need to remember is that you can't expect & guarantee which thread runs & finishes first (unless you are running infinite loop), you can look here to start and understand more about threads.

Comments

0

Yes, that's possible. If you want your thread to be terminated, you should invoke setDaemon method on the thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.