Thread: Basic unit of CPU utilization.
Purpose — Threads let us avoid incurring the overhead of creating and managing separate PCBs.
A thread is much lighter than a PCB and contains:
It shares the following with other threads in the same process.
Example — A word processor may have a thread for getting keystrokes from the user, another for spellcheck, and another for graphics.
Key Difference: In the multithreaded process, the Code, Data, and Heap are shared, while each thread gets its own private Stack, Registers, and Program Counter (PC).
Aside — Today, kernels and modern applications are generally multithreaded.
Concurrency: Supports more than one task making progress.
Parallelism: System can do more than one task simultaneously.
Data Parallelism: Splits data across multiple cores, doing same operation on each. You want to sum an array on a dual-core system. You could have one core sum-up the first half, and the other core do the latter half.Example: Data Parallelism
Task Parallelism: Distribute threads across cores. Each thread can do unique operations on same or different data.
Multicore or multiprocessor systems put pressure on programmers, such as:
Identifies performance gains from adding additional cores to an application that has serial and parallel components.
\text{speedup} \le \frac{1}{ S + \frac{1-S}{N} } \\~\\ \small \textit{where $S$ is serial portion; $N$ is processing cores}
The serial portion has a disproportionate effect on performance.
User: Supported above the kernel, managed without kernel support.
Kernel: Managed directly by the kernel.
Aside — All modern operating systems support multithreading.
Examples — Solaris Green Threads Library
Examples — Windows, Linux, and many programming languages.
Examples — Windows with ThreadFiber package
Examples — Tru64 UNIX
Provides programmer with API for creating and managing threads
Two Primary Implementation:
Examples:
- Windows: kernel-level
- Java: typically kernel-level
- Pthread: any
Managed by the JVM
You can create a thread by implementing the Runnable interface.
class Mytask extends SomeOtherClass implements Runnable {
// ...
}Note — Don’t extend the Thread class, you’ll lock-out extending anything else!