Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a Java Thread reach the 'Die' State

In Java, Die is one of the states on a thread.

What causes a thread to enter this state?

like image 244
Johanna Avatar asked Aug 13 '09 03:08

Johanna


People also ask

How does a thread die in Java?

A thread dies naturally when its run() method exits normally. For example, the while loop in this method is a finite loop--it will iterate 100 times and then exit. A thread with this run() method will die naturally after the loop and the run() method completes.

Does a thread automatically be 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.

When a Java thread is in running state it can be moved to?

In this case, Thread will be moved from “ Running ” state to “ Waiting ” state with specified waiting time. This can be done whenever we want currently running thread to wait for some specified amount of time. Once the specified amount of time is completed, then the waited thread will be moved to “ Runnable ” state.

What are the states of thread life cycle in Java?

The active state contains two states within it: one is runnable, and the other is running. Runnable: A thread, that is ready to run is then moved to the runnable state. In the runnable state, the thread may be running or may be ready to run at any given instant of time.


2 Answers

From the Thread API, here is a complete list:

  • If the run() method returns.
  • If an exception is thrown that propagates beyond the run method.
  • If it is a daemon thread and all non-daemon threads have 'died'
  • If the exit method of class Runtime has been called (even at another thread).
like image 55
idrosid Avatar answered Oct 06 '22 16:10

idrosid


All Threads die either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

like image 21
Rob Avatar answered Oct 06 '22 17:10

Rob