Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is it possible that a parent thread dies before the child ?  RSS feed

 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,
Can anybody please tell me that is it possible that a parent thread dies before the child ones?
Or would you poeple please tell me the order of termination of Threads?


Explanation:

Say i have a Thread "Parent" in it i am starting another two threads say "child1" and "child2" then both childs are still in running phase and Parent Thread dies.




All kinds of Suggestions and Replies are warmly welcomed.
Regards,
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amir,

All Threads are 'unrelated', there are no parent and child threads. So once the 'parent' thread kicks off 'child1' and 'child2', then the three threads are independent. They can end in any order and they don't affect the running of other threads. Which means 'parent' can die before 'child1' and 'child2'.

There are some modifications to this. For example, if 'parent' is the last non-daemon thread running (and thus 'child1' and child2' threads are daemon threads), then when 'parent' dies, the JVM starts to shutdown, so the 'child' threads would also die. The last modification would be if you have some synchronizing code that prevents 'parent' from dieing until the 'child' threads die, or that signals the 'child' threads to die when the 'parent' does.
 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Steve.

Now i understand the whole concept. Thanks alot again...
 
Rakesh Am
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if 'parent' is the last non-daemon thread running (and thus 'child1' and child2' threads are daemon threads), then when 'parent' dies, the JVM starts to shutdown, so the 'child' threads would also die.

---> I have a similar kind of situation, like I am starting a thread from a method. Method will return a value to caller and thread should to rest of the process.
But what happens is thread is starting the process, it stops in between without throwing any exception or error.


example :


public class ThreadTest{

public String testAbc(){

String s ="Hello";
(new Thread(new MyThread())).start();
return s;
}


public class MyThread implements Runnable {
public void run() {
// Process rest ...

}
}
}

Can anybody tell me why it is happening?
What is the solution for this?

Thanks,
Rakesh
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rakesh, welcome to the JavaRanch!

This is a completely new topic and doesn't really relate to the original, so can you please post it in a new topic? I think it will help draw attention to it. Also when you repost UseCodeTags (there is a button which helps you add them). You will probably need to add more info, like real code that shows what you mean. It is hard for me to understand what the problem is with the code and description you posted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!