Tutorial

Thread Life Cycle in Java - Thread States in Java

Published on August 3, 2022
Default avatar

By Pankaj

Thread Life Cycle in Java - Thread States in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Understanding Thread Life Cycle in Java and Thread States are very important when you are working with Threads and programming for multithreaded environment. From our last tutorial, we can create a java thread class by implementing Runnable interface or by extending Thread class, but to start a java thread, we first have to create the Thread object and call it’s start() method to execute run() method as a thread.

Thread Life Cycle in Java

Below diagram shows different states of thread life cycle in java. We can create a thread in java and start it but how the thread states change from Runnable to Running to Blocked depends on the OS implementation of thread scheduler and java doesn’t have full control on that. Thread Life cycle in java, Thread States in java, thread life cycle

New

When we create a new Thread object using new operator, thread state is New Thread. At this point, thread is not alive and it’s a state internal to Java programming.

Runnable

When we call start() function on Thread object, it’s state is changed to Runnable. The control is given to Thread scheduler to finish it’s execution. Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the OS implementation of thread scheduler.

Running

When thread is executing, it’s state is changed to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running. Then CPU starts executing this thread. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources.

Blocked/Waiting

A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to available. For example producer consumer problem or waiter notifier implementation or IO resources, then it’s state is changed to Waiting. Once the thread wait state is over, it’s state is changed to Runnable and it’s moved back to runnable thread pool.

Dead

Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive. Above are the different states of thread. It’s good to know them and how thread changes it’s state. That’s all for thread life cycle in java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 15, 2018

There is no state called “Running” in Thread.State

- slay.gfw

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 17, 2017

    The way of explanation is good but as of my knowledge blocking/waiting states wont come ad thread life cycle state. It would be grate if you provide with program

    - Jaya Sudhakar

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 20, 2017

      Simple and clear explanation.

      - Nagaraj

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 21, 2016

        its a very good explanation i like your point of view because its show simple … but i think it’s better if u also give a small program to explain entire thing…but thank you somuch it is much more benfical to me for understanding lifecycle of thread…

        - Dolly kumari.

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 11, 2016

          Nice. Explanation is very good. But it’s still better if you explain with a small program.

          - Vani

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            March 23, 2015

            Very nice explanation…

            - Vinay

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              October 3, 2014

              I am new to this field. I think that a thread can be (in one of) Ready or Running or waiting or delayed or blocked states only if the process/program (in which the thread is there) is currently in Ready or Running or Waiting states. In other words, If the program/process is is in hold or finished states then any thread (in that process/program) will not be in any one of the states stated above. Please clarify if I am correct.

              - Jim

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 16, 2014

                it would be better if you explained with a small program…

                - g srinivas

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  June 16, 2014

                  Mr. Pankaj Kumar, I am putting 2 years of gap of exp. can you help me about project, interview questions for exps. i read your blog. it’s nice & good. i like your language, because you have written in simple english.

                  - Pankaj Patil

                    Try DigitalOcean for free

                    Click below to sign up and get $200 of credit to try our products over 60 days!

                    Sign up

                    Join the Tech Talk
                    Success! Thank you! Please check your email for further details.

                    Please complete your information!

                    Get our biweekly newsletter

                    Sign up for Infrastructure as a Newsletter.

                    Hollie's Hub for Good

                    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                    Become a contributor

                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                    Welcome to the developer cloud

                    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                    Learn more
                    DigitalOcean Cloud Control Panel