Thread Scheduling:
Multithreading in Java
Thread Scheduling
Thread scheduling in Java (and operating systems in general) determines
which thread gets access to CPU and kernel resources and when. This
decision-making involves coordination between user-level components and the
operating system.
Thread scheduling operates across two boundaries:
1. Scheduling of User-Level Threads (ULTs) to Kernel-Level Threads (KLTs)
through Lightweight Processes (LWPs), managed by the thread library and
application developer.
2. Scheduling of Kernel-Level Threads by the system scheduler to execute
OS-level functions and access physical CPU resources.
Lightweight Process (LWP)
A Lightweight Process (LWP) is a user-space entity that acts as an interface
between User-Level Threads (ULTs) and the kernel. Each LWP is mapped to a
separate Kernel-Level Thread (KLT).
The thread library decides:
Which ULT runs on which LWP
How long a ULT executes on that LWP
LWP Creation Based on Application Type
I/O-bound applications
When an LWP blocks on I/O, another LWP is required to schedule
remaining ULTs.
Therefore, the number of LWPs equals the number of ULTs.
CPU-bound applications
Thread Scheduling: Multithreading in Java 1
The number of LWPs depends on application design.
Typically fewer LWPs are sufficient since threads spend most time on
computation.
Each LWP is always attached to a separate Kernel-Level Thread.
First Boundary of Thread Scheduling (User-Level
Control)
Beyond setting thread priority and scheduling policy, real-time systems require
two additional controls for ULTs:
1. Contention Scope
2. Allocation Domain
Thread Scheduling: Multithreading in Java 2
These controls are specified by the application developer using the thread
library.
Contention Scope
Contention scope defines how and where threads compete for kernel
resources.
Types of Contention Scope
1. Process Contention Scope (PCS)
Contention occurs only among threads of the same process.
The thread library schedules threads onto available LWPs.
Scheduling is based on preemptive priority scheduling defined by the
application developer.
PCS threads may share one or more LWPs.
A high-priority PCS thread can preempt a lower-priority PCS thread within
the same process only.
2. System Contention Scope (SCS)
Contention occurs among all threads in the system, across processes.
Each SCS thread is mapped to a separate LWP and KLT.
Scheduling is handled entirely by the system scheduler.
The thread library has no control over kernel scheduling.
POSIX Support
In Linux and UNIX, the POSIX Pthread library provides:
pthread_attr_setscope()
This function specifies whether a thread uses PCS or SCS during creation.
Thread Scheduling: Multithreading in Java 3
Allocation Domain
An allocation domain is a set of hardware resources (CPU cores) that a thread
competes for.
In multicore systems, there may be multiple allocation domains, each
containing one or more cores.
A single ULT may belong to one or more allocation domains.
Due to the complexity of hardware–software interactions, allocation domains
are not explicitly controlled by the application developer. Instead, the
operating system provides default mechanisms that influence how threads are
mapped to cores.
Example Scenario
Consider:
Three processes: P1, P2, P3
Ten user-level threads: T1–T10
Single allocation domain
100% CPU resources shared among all processes
CPU allocation depends on:
Contention scope
Thread priority
Scheduling policy
System scheduler decisions
Thread Scheduling: Multithreading in Java 4
Process-wise Contention Analysis
Process P1 (PCS Threads)
Threads: T1, T2, T3 (PCS)
T1 and T2 share one LWP
T3 is assigned a separate LWP
Scheduling behavior:
Thread library schedules T1 and T2 using preemptive priority.
A higher-priority PCS thread can preempt a lower-priority PCS thread.
Thread T1 (P1) cannot preempt thread T3 (P3), even if T1 has higher
priority.
If priorities are equal, scheduling depends on the system scheduler, not
the thread library.
Thread Scheduling: Multithreading in Java 5
Process P2 (SCS Threads)
Threads: T4, T5 (SCS)
Scheduling behavior:
T4 and T5 compete with:
Process P1 (as a whole)
SCS threads T8, T9, T10 of Process P3
The system scheduler treats:
P1
T4
T5
T8
T9
T10
PCS threads T6, T7 (P3)
as separate schedulable entities.
The thread library has no role in scheduling SCS threads to kernel resources.
Process P3 (Mixed PCS and SCS Threads)
PCS threads: T6, T7
SCS threads: T8, T9, T10
Assume the system scheduler allocates 50% CPU to Process P3:
25% for PCS threads
25% for SCS threads
Scheduling behavior:
PCS threads (T6, T7) share 25% CPU, scheduled by the thread library
based on priority.
Thread Scheduling: Multithreading in Java 6
SCS threads (T8, T9, T10) divide the remaining 25%, each using its own
LWP and KLT.
SCS scheduling is done by the system scheduler.
Thread and LWP Relationships
Important Notes
For every system call accessing kernel resources:
A Kernel-Level Thread (KLT) is created
Each KLT is associated with a separate LWP
Thread Count Relationships
Number of KLTs = Total number of LWPs
Total LWPs = LWPs for PCS + LWPs for SCS
LWPs for SCS = Number of SCS threads
LWPs for PCS = Depends on application design
Given Example
Number of SCS threads = 5
Number of LWPs for PCS = 3
Number of LWPs for SCS = 5
Total LWPs = 8
Total KLTs = 8
Advantages of PCS over SCS
All scheduling, synchronization, and context switching occur in user space.
Fewer system calls result in better performance.
PCS threads are cheaper than SCS threads.
Thread Scheduling: Multithreading in Java 7
PCS threads can share LWPs, while each SCS thread requires:
A separate LWP
A separate KLT
Increasing SCS threads increases kernel complexity and overhead.
Practical systems limit the number of SCS threads to be smaller than PCS
threads.
Multiple allocation domains further complicate SCS scheduling and
synchronization.
Second Boundary of Thread Scheduling (Kernel-
Level)
The second boundary involves CPU scheduling by the system scheduler.
The system scheduler treats each Kernel-Level Thread as a separate
process.
It allocates CPU time and kernel resources accordingly.
This scheduling is independent of the thread library.
Thread Scheduling: Multithreading in Java 8