Lesson 43
Thread Scheduler
The Thread Scheduler decides which runnable thread executes next, allocating CPU time based on priority and system availability.
Java Track
Save progress as you learn.
61 lessons
Lesson content
The Thread Scheduler is a JVM component that controls which runnable thread executes and when.
Definition List
- Thread Scheduler: JVM component that determines which thread executes next.
- Thread Scheduling: Selecting a thread from runnable threads for execution.
- CPU Time: Processor time allocated to a thread.
- Context Switching: Switching CPU execution from one thread to another.
- Runnable Thread: A thread ready to execute, waiting for CPU.
Types of Thread Scheduling
- Preemptive Scheduling — Scheduler can pause a running thread.
- Priority-Based Scheduling — Higher-priority threads get preference.
Priority Constants
Thread.MIN_PRIORITY // 1
Thread.NORM_PRIORITY // 5
Thread.MAX_PRIORITY // 10Example 01: Java Thread Scheduler
package com.java.Multi_threading;
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + " - Priority: " + getPriority() + " - Count: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadSchedulerExample {
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread-1 (Low Priority)");
MyThread t2 = new MyThread("Thread-2 (High Priority)");
MyThread t3 = new MyThread("Thread-3 (Medium Priority)");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}Output
Thread-2 (High Priority) - Priority: 10 - Count: 1
Thread-3 (Medium Priority) - Priority: 5 - Count: 1
Thread-1 (Low Priority) - Priority: 1 - Count: 1
Thread-2 (High Priority) - Priority: 10 - Count: 2
Thread-3 (Medium Priority) - Priority: 5 - Count: 2
...Thread Scheduler and Life Cycle
New
↓
Runnable ← Thread Scheduler Selects
↓
Running
↓
Waiting/Blocked
↓
Runnable
↓
TerminatedContext Switching
Thread.sleep(100);Pauses the thread temporarily, releasing CPU to others.
Best Practices
- Never depend on a fixed execution order.
- Use synchronization to protect shared resources.
- Keep threads lightweight for better scheduling.
- Treat priorities as hints, not strict controls.
FAQ
1. What is a thread scheduler in Java?
A JVM component that assigns CPU time to runnable threads.
2. Does Java always run higher-priority threads first?
No. Priority increases likelihood but doesn't guarantee order.
3. What is context switching?
Pausing one thread and switching CPU to another thread.
Where Java is used
Java in real-world development
Thread Scheduler
JVM component that picks which runnable thread executes next.
Preemptive Scheduling
Running thread can be paused for another thread.
Priority-Based Scheduling
Higher-priority threads are generally preferred by the scheduler.
Context Switching
CPU stops one thread and starts another. Thread.sleep() triggers it.
Execution Order
Not guaranteed. JVM and OS both influence final scheduling.
Best Practice
Use synchronization, not priority, to control resource access.