What are the states of a thread?

I have been searching in some places on the internet but I have not found coherence in the definitions given on the subject of thread in Java.

What are the possible states of a thread and what are its definitions.

Author: Maniero, 2016-10-06

2 answers

States of a thread

insert the description of the image here

Image taken from Thread States and Life Cycle .

The states according to documentation are:

  • NEW : when thread is created, but did not invoke start() in the reference.

  • RUNNABLE : when it returns from some state, or when start() was invoked in the reference.

  • BLOCKED:

    1. a thread is considered in the state BLOCKED when it is waiting for data.
    2. a thread is also considered BLOCKED when it is waiting for Lock from another thread.
  • WAITING : a thread that is waiting indefinitely for another thread to perform a certain action is in this state.

  • TIMED_WAITING : a thread that is waiting for another thread to run a action for up to a specified timeout is in this state.

  • TERMINATED : a thread that came out is in this state.

To know the states, in Java you can use the method Thread.getState which returns:

  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

In addition, it can be called isAlive()

  • TRUE means that the thread is in the state Runnable or in the state Non-Runnable.


References:

 11
Author: Taisbevalle, 2016-11-11 00:59:17

If it is Java specific it is easy to find out since the states are defined by an enumeration, then it has all in the documentation of the Thread.State.

  • NEW-it has been created and is ready to start (start())
  • RUNNABLE - it is running (there is no state RUNNING)
  • BLOCKED - it is locked, usually by Lock or some IO operation
  • WAITING - she is waiting for another thread to run
  • TIMED_WAITING-same thing, but there is a time limit she will wait
  • TERMINATED - it ended execution, but still exists (there is no state DEAD)

Won't vary much from this, but other implementations may use another set of states.

Note that the inconsistencies found are probably because people are talking about different things. I keep the documentation, it's certainly not wrong. When you find inconsistency always go into what it is official.

If not talking about Java the states may vary. Each platform can have its own control, including different from the operating system. If you're curious see the states of threads possible in C#.

 9
Author: Maniero, 2020-11-11 13:49:56