Yves
Yves

Reputation: 12371

java threads execution order

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.

Upvotes: 0

Views: 98

Answers (3)

Vasu
Vasu

Reputation: 22422

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.

Upvotes: 1

dhke
dhke

Reputation: 15388

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.

Upvotes: 1

Farrandu
Farrandu

Reputation: 371

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

Upvotes: 0

Related Questions