Home »
Java programming language
Java Thread Life Cycle and States
In this tutorial, we will learn about the thread life cycle, what is the life cycle of a thread in Java? What are the different states in the life cycle of a thread in Java?
By Preeti Jain Last updated : January 26, 2024
Thread Life Cycle & States
The following are the various states (lifecycle) of Java thread:
- New State
- Ready State
- Running State
- Dead or Terminated State
- Running → Ready
- Waiting State
- Sleeping State
- 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:
- If waiting thread got interrupted.
- 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):
- If time expires.
- If sleeping thread got interrupted.
Syntax:
Thread.sleep();
8. 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.w3-code();