mhamad arsalan
mhamad arsalan

Reputation: 87

Output of main thread shown after using System.exit() inside a thread

I'm about learning Threads in Java. I just created a new thread it's fine there, I want program to be closed when some operation is done, but when I call System.exit(0); inside thread, the program won't close!

code:

public class Humaida
{
    public Thread thread;
    public Humaida()
    {
        thread = new Thread(() ->              //i guess it using lambda expression,i just copied it
        {
            try
            {
                System.out.println("inside thread");
                System.exit(0);            // i think this line have to close\terminate JVM or the whole program
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        });
        thread.start();                           //IMO this should be last line to execute

        System.out.println("after thread.start"); //shouldn't be run cause System.exit(0); reached

        printIt("after reading thread");          // i used my method just to ensure that program running
    }

    public static void main(String[] args)
    {
        new Humaida();
    }

    public void printIt(String text)
    {
        System.out.println(text);
    }
}

I get some strange output like

1st run:
after thread.start
inside thread
Press any key to continue . . .

 2nd run:
after thread.start
after reading thread
inside thread
Press any key to continue . . .

3rd run:
after thread.start
inside thread
after reading thread
Press any key to continue . . .

Let's forget the different output for same code, that isanother problem.

I searched for some solution, I tried System.exit(-1); and thread.kill() but it doesn't work either.

What is happening here?

Does System.exit(0); just kill this thread, and not the main thread?

Upvotes: 0

Views: 454

Answers (2)

Steve11235
Steve11235

Reputation: 2923

For educational purposes, try putting another println("Say what?") after the System.exit(). If "Say what?" displays, something is definitely wrong.

You've created a race condition between the two threads. How far the first thread gets is dependent on how quickly the new thread runs. The only thing that is guaranteed to display is "inside thread".

Upvotes: 0

that other guy
that other guy

Reputation: 123480

The point of threads is to do things in parallel. What you're seeing is a race condition.

When you do thread.start();, it'll start running the code in the thread but also continue immediately with the System.out.println("after thread.start"); statement. This is why you may see both.

If you want to wait until the thread has finished, you can do:

    thread.start();                           
    try {
      System.out.println("Main thread waiting for new thread to finish");
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

In this case, you'll see that the entire VM exits before the thread has finished.

Upvotes: 1

Related Questions