• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Steve.

Now i understand the whole concept. Thanks alot again...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic