Friday, 7 March 2014

Will main thread waits for all it's child threads to complete?

There is lots of confusion whether main thread waits for all it's child threads to complete or not?. Generally there is no child thread concept, here it means the threads which are started in the main block.

Already we have discussed it here about Daemon threads and non-daemon threads and main thread will be a non-daemon thread always. Also threads created by main threads also will be a non-daemon threads until explicitly set.  And JVM waits for all the non-daemon threads to complete.

So comes to our question, will main thread waits for all it's child threads to complete?
I would like to explain it with the below example.
Example
package thread;
/**
* @author Sivaranjani D
*
*/
public class IsMainThreadAlive {

public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
System.out.println("Current thread is "+mainThread.getName());
Runnable target = new targetCode(mainThread, "Thread1");
Thread t1 = new Thread(target);
t1.start();
System.out.println("Thread "+mainThread.getName()+" is alive in main method? "+ mainThread.isAlive());
}

}

class targetCode implements Runnable{
Thread mainThread;
Thread name;
public targetCode(Thread mainThread,String Name){
this.mainThread = mainThread;
this.name=name;
}

public void run() {

for(int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+" is executing : "+i);
}

System.out.println("Thread "+mainThread.getName()+" is alive at the end of child thread? "+ mainThread.isAlive());
}

}

output
Current thread is main
Thread main is alive in main method? true
Thread-0 is executing : 0
Thread-0 is executing : 1
Thread-0 is executing : 2
Thread-0 is executing : 3
Thread-0 is executing : 4
Thread-0 is executing : 5
Thread-0 is executing : 6
Thread-0 is executing : 7
.
.
.
.
.
.
Thread-0 is executing : 43
Thread-0 is executing : 44
Thread-0 is executing : 45
Thread-0 is executing : 46
Thread-0 is executing : 47
Thread-0 is executing : 48
Thread-0 is executing : 49
Thread main is alive at the end of child thread? false

Code Explanation

  1. When the main method starts, the main thread starts. So we are getting the current thread and storing it in the variable mainThread.

  2. when we print the stored thread's name it shows "main". This step is to ensure that we have stored the main thread in the variable mainThread.

  3. Creating the runnable object by passing the main thread and that way we are passing the main threads reference to the runnable object.

  4. In run method we are setting some loop to print the values and at the end we are checking whether main thread is alive or not

  5. We have created a thread and passed the runnable object.

  6. we are checking the main thread is alive or not at the end of the main method.


Conclusion:
So from the output, we can see that, the main thread is not running at the end of the child thread because calling isAlive() is false at the end of the child thread.

No comments:

Post a Comment