views:

509

answers:

4

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

What causes a thread to enter this state?

+5  A: 

when the run() method returns

Edit: If a thread is marked as "daemon" it may die before the run() method returns, if all other non-daemon threads have died.

Edit 2: After reading the comments, the other answers and 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).
idrosid
I like the comment by Rob about exceptions propagating beyond the run method. I would add that to your list.
TheJacobTaylor
+2  A: 

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

Rob
A: 

Threads die in the following situations:

  1. When the method it runs finishes (or throws)
  2. When the process is terminated
  3. When the computer is turned off or reset.
Spencer Ruport
+1  A: 

There are two ways for a thread to die:

a) It could die of natural causes which is when the run() method finishes or return,

or

b) it could be kill by using the stop() method or when something goes wrong with the program(This could be an Exception) or computer.

chermosillo