Home » Java programming language

Explain life cycle of a thread in java



In this article, we are going to learn about thread life cycle, what is life cycle of a thread in java? What are the different states in life cycle of a thread in java?
Submitted by Preeti Jain, on February 06, 2018

Thread Life Cycle (different states of a thread)

There are few states of Thread life cycle,

  1. New State
  2. Ready State
  3. Running State
  4. Dead State
  5. Waiting State
  6. Sleeping State
  7. Suspended State

We will study all the states described above:

1) New state

When thread is instantiated or created then thread will be in new state.

Syntax:

NewThread nt = new NewThread();

2) Ready state

When we call start() method on thread object then our thread will be in ready state (i.e. get ready to enter in running state).

Syntax:

nt.start();

3) Running state

Here, we discuss one thing before entering in running state. What is the role of thread scheduler thread scheduler allocates processor to the waiting threads based on priority. Our thread will be in running state when thread scheduler allocate processor to the thread means that thread will get a chance to execute.

4) Dead or Terminated state

When run() method completes their execution then our thread will be in dead state or last state.

5) Running → Ready

If a running thread calls yield() method then our running thread will be in running to ready state because yield() stop executing and give a chance to other waiting thread.

Syntax:

Thread.yield();

6) Waiting state

If a running thread calls join() method then our thread will be in waiting state because join() waits until completing some other thread. If thread comes out from (waiting to ready) state there are two conditions:

  1. If waiting thread got interrupted.
  2. If time expires.

Syntax:

obj.join();

7) Sleeping state

If a running thread calls sleep() method then our thread will enter into sleeping state and there are few cases when sleeping thread come out from (sleeping to ready state):

  1. If time expires.
  2. If sleeping thread got interrupted.

Syntax:

Thread.sleep();

7) Suspended state

When a running thread call suspend() method then our running thread will be in suspended state and suspended thread come out in ready state by calling resume() method.

Syntax:

obj.suspend();

Read more...

  1. How to Create Java Threads (java examples to create threads)?
  2. Thread Synchronisation in Java with Example.
  3. Java program to demonstrate example of thread.
  4. Java program to Join Threads.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.