What are the states of a thread?

Asked

Viewed 3,382 times

14

I have been researching some places on the Internet but I did not find consistency in the definitions given on the subject of thread in Java.

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

  • 1

    After that already had answer becomes complicated to change the context of the question :/

  • @Renan thanks for your attention anyway!

2 answers

10

If it is specific to Java it is easy to find out since the states are defined by an enumeration, then it has all in the documentation of 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, in general by Lock or some operation of IO
  • WAITING - she’s waiting for another thread to rotate
  • TIMED_WAITING - the same thing, but there’s a time limit she’ll wait
  • TERMINATED - it finished execution, but still exists (no status exists DEAD)

It won’t vary much from this, but other implementations can use another set of states.

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

If you are 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 states of threads possible in C#.

10

States of a thread

inserir a descrição da imagem aqui

Image taken from Thread States and Life Cycle.

The states, according to the documentation sane:

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

  • RUNNABLE : When returning from any state, or when the start() in the reference.

  • BLOCKED:

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

  • TIMED_WAITING: One thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

  • TERMINATED: One thread who left is in this state.

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

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

Besides, you can call it isAlive()

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


References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.