Java thread execution order

advertisements
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.


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.