Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java if one thread got killed, what will happen to the other thread?

I want to know in Java:

  1. If main thread got killed what will happen to other children threads?
  2. If child thread got killed what will happen to siblings and parent thread?

I read in the following link that since threads sharing address space, killing one thread can affect other thread also.

Below is a quote from here.

Threads are light weight processes that divide main flow of control into multiple flows and each flow of control/thread will execute independently. Activity of the process in a system is represented by threads. The process that has multiple threads is called as multi threaded. Each threads has its own thread ID ( Data Type Integer), register, program counter, stack, error no. Threads can communicate using shared memory within same process. There are different advantages of using threads to mange and maintain the subtask of applications. When we are using threads than less system resources are used for context switching and increased the throughput of application. Threads also simplify the structure of program. There is no special mechanism for communication between tasks. Threads also have some disadvantages for example threads are not reusable as they are dependent on a process and cannot be separated from the process. Threads are not isolated as they don't have their own address space. The error cause by the thread can kill the entire process or program because that error affects the entire memory space of all threads use in that process or program. Due to the shared resources by the threads with in the process can also affect the whole process or program when a resource damage by the thread. For concurrent read and write access to the memory thread will required synchronizations. Data of the process can easily damage by the thread through data race because all the threads with in the process have write access to same piece of data.

Can u gys please tell whether whatever told in the above link is applicable to java

like image 841
nantitv Avatar asked Nov 18 '11 09:11

nantitv


People also ask

What happens when a thread is killed?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads.

Can one thread stop another thread?

You can't stop a thread from another thread. You can only ask the thread to stop itself, and the best way to do that is to interrupt it. The interrupted thread must collaborate, though, and respond to the interruption as soon as possible by stopping its execution.

Can one thread block another thread in Java?

No, this is impossible. It's hard to imagine why you would ever need it. Let's say there was a notifyOtherThreadAndBlock() call. This call would notify the other thread and then block.

Can main thread dies before the child thread?

In java Thread join method is used, so that main or parent thread can wait for its child thread to finish its execution and die. After that only main thread can executes its further statements.


1 Answers

1) Nothing will happen to the "child threads"...

2) Nothing will happen to the "sibling threads"...

...with the following exception: If all remaining threads are daemon threads, the application will terminate (i.e., when there are only daemon threads left, these will be killed as well).

From the documentation of Thread:

[...] The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called [...]
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
like image 182
aioobe Avatar answered Sep 30 '22 08:09

aioobe