- Learning Objectives
- Prerequisites
- Introduction
- What is a Thread Life Cycle?
- Thread States
- State Transitions
- Thread Scheduler
- Context Switching
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand the Thread Life Cycle.
- Learn different thread states.
- Understand state transitions.
- Learn how the Thread Scheduler works.
- Understand Context Switching.
- Learn the complete execution flow of a thread.
Before learning this chapter, you should know:
- Threads
- Multithreading
- Thread Creation
- start()
- run()
- join()
A thread does not start executing immediately after it is created.
Instead,
it passes through several states before completing execution.
These states collectively form the Thread Life Cycle.
Understanding the Thread Life Cycle helps developers write efficient multithreaded programs and debug thread-related issues.
The Thread Life Cycle is the sequence of states through which a thread passes from its creation until its termination.
The Thread Life Cycle represents the journey of a thread from creation to completion.
A thread generally passes through the following states.
New
↓
Runnable
↓
Running
↓
Blocked / Waiting
↓
Runnable
↓
Running
↓
Terminated
A thread is in the New state immediately after the thread object is created.
Example
thread = threading.Thread(target=display)At this stage,
the thread exists,
but it has not started execution.
After calling
thread.start()the thread enters the Runnable state.
The thread is ready to execute,
but it is waiting for CPU time from the operating system scheduler.
When the operating system scheduler assigns CPU time,
the thread moves to the Running state.
Its task begins executing.
Example
def display():
print("Running")A running thread enters the Blocked or Waiting state when it cannot continue immediately.
Examples include:
- Waiting for I/O operations.
- Waiting for a lock.
- Waiting for another thread using
join(). - Sleeping using
time.sleep().
After the required resource becomes available,
the thread moves back to the Runnable state,
waiting once again for CPU allocation.
A thread enters the Terminated state after completing its task.
Once terminated,
the thread cannot be restarted.
Attempting to call
start()again raises an error.
New
↓
Runnable
↓
Running
↓
Blocked / Waiting
↓
Runnable
↓
Running
↓
Terminated
| Current State | Event | Next State |
|---|---|---|
| New | start() |
Runnable |
| Runnable | CPU Allocated | Running |
| Running | Waiting for Resource | Blocked |
| Blocked | Resource Available | Runnable |
| Running | Task Completed | Terminated |
The Thread Scheduler is responsible for deciding which runnable thread gets CPU time.
The scheduler is controlled by the operating system.
Python programmers do not directly control thread scheduling.
Therefore,
the execution order of multiple threads is generally not predictable.
When multiple threads share a CPU,
the operating system rapidly switches execution between them.
This process is called Context Switching.
Example
Thread A
↓
Thread B
↓
Thread C
↓
Thread A
↓
Thread B
Rapid switching creates the appearance that threads are executing simultaneously.
Thread Object Created
↓
New State
↓
start()
↓
Runnable
↓
CPU Assigned
↓
Running
↓
Waiting?
↓
Yes
↓
Blocked
↓
Resource Available
↓
Runnable
↓
Running
↓
Task Finished
↓
Terminated
- Helps understand thread execution.
- Simplifies debugging.
- Makes synchronization easier.
- Improves understanding of scheduler behavior.
- Helps design efficient multithreaded applications.
- Start a thread only once.
- Use
join()when thread completion is required. - Avoid unnecessary waiting.
- Keep thread tasks short and efficient.
- Synchronize shared resources properly.
Trying to restart a terminated thread.
❌ Incorrect
thread.start()
thread.start()A thread can be started only once.
Assuming a thread starts immediately after calling start().
The thread first enters the Runnable state and waits for CPU scheduling.
Assuming execution order is fixed.
The operating system scheduler determines the execution order.
Ignoring blocked states while designing applications.
Waiting for resources can affect responsiveness.
It is the sequence of states through which a thread passes from creation until termination.
New.
start()The operating system's Thread Scheduler.
No.
A thread can be started only once.
Context Switching is the process of rapidly switching CPU execution between multiple threads.
Because thread scheduling is controlled by the operating system.
- New
- Runnable
- Running
- Blocked / Waiting
- Runnable
- Running
- Terminated
start()- Scheduler
- Context Switching
In this chapter, you learned:
- Thread Life Cycle
- Thread States
- State Transitions
- Thread Scheduler
- Context Switching
- Internal Working
- Advantages
- Best Practices
- Common Mistakes
- Interview Questions
- Quick Revision
Write a Python program to demonstrate the New state of a thread.
import threading
def display():
print("Child Thread")
thread = threading.Thread(target=display)
print("Thread Created")Thread Created
The thread object has been created,
but start() has not been called.
Therefore,
the thread remains in the New state.
import threading
def display():
print("Thread is Ready to Execute")
thread = threading.Thread(target=display)
thread.start()
thread.join()Thread is Ready to Execute
After calling
start()the thread enters the Runnable state.
The operating system scheduler later selects it for execution.
import threading
def display():
print("Thread is Running")
thread = threading.Thread(target=display)
thread.start()
thread.join()Thread is Running
When CPU time is assigned,
the thread enters the Running state and executes its task.
import threading
import time
def display():
print("Running")
time.sleep(3)
print("Resumed")
thread = threading.Thread(target=display)
thread.start()
thread.join()Running
Resumed
During
time.sleep(3)the thread enters the Waiting state.
After three seconds,
it becomes runnable again.
import threading
import time
def child():
print("Child Started")
time.sleep(2)
print("Child Finished")
thread = threading.Thread(target=child)
thread.start()
thread.join()
print("Main Thread Finished")Child Started
Child Finished
Main Thread Finished
The main thread waits until the child thread completes.
import threading
def task():
print("Executing")
thread1 = threading.Thread(target=task)
thread2 = threading.Thread(target=task)
thread3 = threading.Thread(target=task)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()Executing
Executing
Executing
All three threads enter the Runnable state.
The scheduler decides which thread executes first.
import threading
import time
def task(name):
for i in range(3):
print(name, i)
time.sleep(1)
thread1 = threading.Thread(target=task, args=("A",))
thread2 = threading.Thread(target=task, args=("B",))
thread1.start()
thread2.start()
thread1.join()
thread2.join()A 0
B 0
A 1
B 1
A 2
B 2
The operating system rapidly switches between threads.
This process is called Context Switching.
The exact output order may vary.
import threading
def task():
print("Task Completed")
thread = threading.Thread(target=task)
thread.start()
thread.join()
print("Thread Terminated")Task Completed
Thread Terminated
After completing its task,
the thread enters the Terminated state.
import threading
def task():
print("Executing")
thread = threading.Thread(target=task)
thread.start()
thread.join()
thread.start()RuntimeError:
threads can only be started once
A thread can be started only once.
After termination,
it cannot be restarted.
import threading
import time
def task():
print("Running")
time.sleep(2)
print("Completed")
thread = threading.Thread(target=task)
print("New State")
thread.start()
thread.join()
print("Terminated")New State
Running
Completed
Terminated
The thread progresses through the following states:
New
↓
Runnable
↓
Running
↓
Waiting
↓
Running
↓
Terminated
Thread Object Created
↓
New
↓
start()
↓
Runnable
↓
Scheduler
↓
Running
↓
sleep()
↓
Waiting
↓
Runnable
↓
Running
↓
Completed
↓
Terminated
Process
│
├── Main Thread
│
└── Child Thread
│
├── New
├── Runnable
├── Running
├── Waiting
└── Terminated
- Helps understand thread execution.
- Makes debugging easier.
- Improves synchronization design.
- Explains scheduler behavior.
- Useful for interview preparation.
- Start each thread only once.
- Use
join()whenever synchronization is required. - Avoid long blocking operations inside threads.
- Minimize unnecessary waiting.
- Design thread tasks to be independent.
Restarting a completed thread.
❌ Incorrect
thread.start()
thread.start()Assuming start() immediately executes the thread.
The thread first enters the Runnable state and waits for the scheduler.
Expecting a fixed execution order.
Thread scheduling depends on the operating system.
Ignoring waiting states.
Operations such as sleep() and join() temporarily block thread execution.
- New
- Runnable
- Running
- Blocked / Waiting
- Terminated
start()The operating system's Thread Scheduler.
Context Switching is the process of switching CPU execution between multiple threads.
No.
A thread can only be started once.
time.sleep()It makes one thread wait until another thread completes execution.
- Demonstrate all thread lifecycle states.
- Create multiple runnable threads.
- Demonstrate
sleep(). - Demonstrate
join(). - Show that restarting a thread raises an error.
- New
- Runnable
- Running
- Waiting
- Terminated
- Scheduler
- Context Switching
sleep()join()
In this chapter, we learned:
- Thread Life Cycle
- Thread States
- Scheduler
- Context Switching
- Waiting State
- Thread Termination
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision