Warm tip: This article is reproduced from serverfault.com, please click

Does a process dies when the thread it created dies?

发布于 2020-12-02 05:21:10

I have a class in Java which has main() function. Lets call it A. There is another class B which implements Runnable interface. In main function of class A, I create a thread of class B and that thread will start executing its logic.

Now if lets say because of some error, the thread of class B died. Then would the process A dies along with that ? If not how can we make process A die when thread of class B dies.

Is vice versa possible, like if process A dies, then would thread of class B dies along with it ? and if not how to make it die ?

Questioner
H S Reddy
Viewed
0
papaya 2020-12-03 14:41:09

Your question is on the topic of how to catch a child thread's exception and raise it in the parent thread.

Technically like the other answer, there is no relationship between what broke in the child thread which indeed raises an exception in the parent thread.

public static void main(String[] args){
 
  Thread child = new Thread(){
    public void run () {
            try {
                Object obj = null;
                String s = obj.toString(); //this will throw NullPointer
            } catch (NullPointerException e) {
                throw new RuntimeException("Hoping this reaches the parent! as NPE but it wont", e);
            }
        }
  };
  child.start();
}

However to answer your question on how to rather do it. You can employ the use of Future<?> and have a Runnable or Callable do the equivalent and see the values of the Future Result

Here's an example which can call doSomething():

void doSomething(){
  Object s = null;
  System.out.println("You want to do something here!" +  s.toString()); // This will throw NPE
}

And now lets say you want to run this in a separate thread and catch the exception

ExecutorService executor = Executors.newFixedThreadPool(1);
Future result = pool.submit(this::doSomething());
try {
   result.get();
}catch (ExcecutionException e){
  e.printStackTrace(); //will show null pointer exception 
}

Now, your function which ran in a separate thread from the threadPool will throw an execution exception which you can chose to catch at the main/parent thread and do what you please with that failure.

Hoping this will help!


UPDATE:

Why is killing parent thread from child thread bad practise?

It's a really bad idea to terminate any thread from outside that thread. This is because the terminating thread has no idea what the target is doing and whether it is at a "safe" point to terminate. For example you might be in the middle of writing output, or making calculations, or anything. ref.1

There is never really a good candidate on why you should even stop/kill/terminate the entire JVM either. Unless in my ongoing journey of mastering java and core concepts, never have I ever had to deal with a child process killing a parent.

Just to elaborate, specifically, there is infact no concept of "child" thread or "parent" thread. JVM doesn't even keep track of which thread created which thread either.

Its mostly all based on thread groups eg. "groupA" and "groupB" and there is only a "groupA" created "groupB" which JVM knows and keeps track off.

If you really wish to stop the JVM. Just call System.exit ref.2

Consider having a look at this question also