Frosted Cupcake
Frosted Cupcake

Reputation: 1970

How threads are executed in java

I have a code-

public class ThreadOne
{
    public static void main(String[] args)
    {
        Thread1 th=new Thread1();
        Thread1 th2=new Thread1();
        th.start();
        th2.start();
        System.exit(1);
    }
}


class Thread1 extends Thread
{
    public void run()
    {
        for(int i=0;i<10;i++)
        {
            System.out.println(i);
        }
    }
}

What I wanted to know is-

Upvotes: 1

Views: 142

Answers (8)

chengpohi
chengpohi

Reputation: 14217

System.exit(1); will terminate the currently running Java Virtual Machine. When your program exit, your threads will also die.

Thread is a part of Process, If Process have exited, then all threads will be destroyed.

Thread.join() will wait until thread run finished.

public class ThreadOne
{
    public static void main(String[] args)
    {
        Thread1 th=new Thread1();
        Thread1 th2=new Thread1();
        th.start();
        th2.start();
        th.join();
        th2.join();
        System.exit(1);
    }
}


class Thread1 extends Thread
{
    public void run()
    {
        for(int i=0;i<10;i++)
        {
            System.out.println(i);
        }
    }
} 

Upvotes: 5

Rahul Prasad
Rahul Prasad

Reputation: 757

To see the numbers being printed on screen add the following:

try{
       th.join();
       th2.join();
   }catch(Exception e){
}

After th2.start() in main method.

As others have pointed out you will need to give time to the user-defined threads to complete their work and this is done by calling join() on the respective thread objects.join() ensures that the calling thread "Waits for this thread to die" before proceeding ahead.

Upvotes: 0

Mark
Mark

Reputation: 863

According to java docs regarding system.exit

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

So above is your answer regarding your first question. When JVM starts usually a single thread is created which calls main and then rest of your methods. Hope it helps.

Upvotes: 1

Remo Remo
Remo Remo

Reputation: 33

you can wait till the threads executes by using join.check the following

 public class ThreadOne
 {
public static void main(String[] args)
{
    Thread1 th=new Thread1();
    Thread1 th2=new Thread1();
    th.start();
    th2.start();
    try {
        th.join();
        th2.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.exit(1);
}

}

  class Thread1 extends Thread
{
public void run()
{
    for(int i=0;i<10;i++)
    {
        System.out.println(i);
    }
}

}

Upvotes: 1

Kanagaraj M
Kanagaraj M

Reputation: 966

As thread is not Synchronized execution like normal java code. so after calling th.start(); th2.start(); it won't wait for run() to complete thats why System.exit(1); called and you are getting anything.

Upvotes: 1

Paj
Paj

Reputation: 11

Since the threads are separate from main: the code in main continues to be executed and System.exit(1); executes without any regard for the Threads and shuts down the program.

Upvotes: 0

kuhajeyan
kuhajeyan

Reputation: 11017

your 2 threads are started by the main threads, so there are 3 threads get invoked. but system.exit() kills your main thread causing other 2 threads to terminate before they get any chance to run.

Upvotes: 0

BladeCoder
BladeCoder

Reputation: 12929

  1. Your code does not show anything because you are killing the app with System.exit() as soon as it starts. You should wait for both threads to complete before exiting, by using Thread.join() for example.

  2. There is one thread by default, which executes your main() method and it is created by the JVM.

Upvotes: 1

Related Questions