What are the states of a thread?

13

I've been searching in some places on the internet but I have not found any 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.

    
asked by anonymous 06.10.2016 / 16:03

2 answers

10

States of a thread

Imagetakenfrom Thread States and Life Cycle .

States, according to documentation are: / p>

  • NEW : When thread is created, however, they did not invoke start() in the reference.

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

  • BLOCKED :

  • A thread is considered in the BLOCKED state when waiting for data.
  • A thread is also considered BLOCKED when waiting for Lock of another thread.
  • WAITING : A thread that is waiting indefinitely for another thread to perform a given action is in this state.
  • TIMED_WAITING : A thread that is waiting for another thread to perform an action for up to a specified timeout is in this state.

  • TERMINATED : A thread that has gone out is in this state.

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

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

What's more, you can call isAlive()

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


References:

06.10.2016 / 16:55
8

If it is specific to Java it is easy to find out that states are defined by an enumeration, so everyone has documentation of Thread.State .

  • NEW - it was 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 - it is waiting for another thread to run
  • TIMED_WAITING - same thing, but there is a time limit it will wait
  • TERMINATED - it has finished execution, but it still exists (there is no status DEAD )

It will not 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 get the documentation, certainly not wrong. When you find inconsistency always go in what is official.

If you are not talking about Java the states may vary. Each platform can have its own control, even different from the operating system. If you're curious, check out threads in> possible in C # .

    
06.10.2016 / 16:17