25

This is a pretty basic question about the vocabulary of Java threads.

I can't see any possible duplicates but there might be.

What does the word alive refer to in Oracles documentation?

Is it when the run() method has not yet completed or is it any other parameter?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
  • 12
    The answer is there: "*A thread is alive if it has been started and has not yet died.*". This link should help you too: http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/states.html – Maroun Jun 25 '13 at 09:10

7 Answers7

31

According to the Javadoc you mentionned:

A thread is alive if it has been started and has not yet died.

A thread "starts" when its start() method is invoked and "dies" at the end of its run() method, or when stop() (now deprecated) is invoked. So yes, a thread is "alive" when its run() method is still ongoing, but it is also "alive" in the time window between the invocation of start() and the implicit invocation of the run() method by the JVM.

You can also check the Thread.getState() and interesting information about Thread States suggested by @Marou Maroun.

I am also following his suggestion warning you that a Thread can end prematurely in case an Exception is thrown that propagates beyond run. The Thread would not be alive anymore in that case.

EDIT: As suggested by @zakkak, the thread can be considered alive even though the run() method did not start yet. In case you want to have proper control on when it will be invoked, use the ScheduledExecutorService, specifically the schedule() method which gives you more precise execution schedule.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • I noticed that you added it, I deleted my comment. You should note too that an exception could be thrown and kill the thread. – Maroun Jun 25 '13 at 09:24
  • 1
    I updated my answer to include your link and your comment. It is now half yours :) – Matthieu Jun 25 '13 at 09:29
  • 4
    It's all yours :) Well explained answer. – Maroun Jun 25 '13 at 09:31
  • 4
    Just note that `Thread#stop` is nothing more than throwing an exception asynchronously. If that exception doesn't result in the run method aborting, the thread lives on. – Marko Topolnik Jun 25 '13 at 09:38
  • 1
    @MarounMaroun I've recently realized this, spreading the word now :) The documentation does state it, but in such a roundabout way that it sounds like the exception is thrown with no chance of catching it. – Marko Topolnik Jun 25 '13 at 09:41
  • I am not sure that "has been started and has not yet died" means its `run()` method is running, i.e., it may have started `start()` but not been scheduled yet, so `run()` did not yet start running. – zakkak Feb 06 '16 at 16:32
  • @zakkak it's a quote from the javadoc. Once `start()` has been called, you have no control as to when the thread will be started but it should be no more than a few ms, according to your JVM. – Matthieu Feb 06 '16 at 18:21
  • @Matthieu you are right, but depending on the context those few ms (or maybe more, if you oversubscribe threads to cores) might be very important, so the answer might be misleading. – zakkak Feb 06 '16 at 19:26
  • 2
    @zakkak yet, you have to make some asumptions: as the javadoc states, once you called `start()`, you must assume your thread is started. If you need precise control on your `run()`, you should use a `Runnable` in a `ScheduledExecutorService`, or an OS with a Real Time scheduler (with the appropriate JVM). I'll see how I can update my answer with that in mind. – Matthieu Feb 06 '16 at 19:44
  • @zakkak thanks for the edit! You made it before I could grab a computer ;) I might also add about the `ScheduledExecutorService` to have more control on the "real" thread start. – Matthieu Feb 07 '16 at 08:31
4

Thread is alive after start() returned and until run() returns to JVM.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

A thread is alive when it is in new/Running/wait state. In essence the run method can be running or not

sidshu
  • 315
  • 4
  • 13
  • You have to `start()` the Thread in order for it to be alive: `MyThread t = new MyThread(); t.isAlive();` returns 'false' – Matthieu Jun 25 '13 at 09:22
1

A thread is alive when the start method is called on it and before it is dead. It can move to waiting state number of times before it is dead, even if it is in the waiting state it is still alive.

From being alive to being dead, it can move from runnable state to waiting state.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

A thread is considered to be alive from the moment you create a Thread object and start it until it completes the run method or dies. It will be said to be in alive state even if it is in idle, running or sleep state.

nevets1219
  • 7,692
  • 4
  • 32
  • 47
Prabhat
  • 810
  • 9
  • 33
0

Thread is "Alive" means that Thread is still running.

Yes, you can say that run() method is executing for the Alive Thread.

Moreover, isAlive() method is used to know if the Thread is still running.

final boolean isAlive()

The isAlive() method returns true if the thread upon which it is called is still running. It returns false otherwise.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
Nargis
  • 4,687
  • 1
  • 28
  • 45
0

A thread is alive when it is in New State or in Running/Wait State.And we can also tell that until thread is died, it is alive in memory.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17