Thread
Definition
A thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. Multiple threads can exist within a single process, executing concurrently and sharing resources such as memory.
Why It Matters
Multi-threading allows a program to perform multiple tasks at the same time, which can dramatically improve performance and responsiveness. For example, one thread can handle the user interface while another performs a long-running calculation in the background.
Contextual Example
In a word processor, one thread listens for your keyboard input, another thread checks your spelling as you type, and a third thread saves your document automatically in the background. All these threads run concurrently within the same word processor application.
Common Misunderstandings
- A process is a running instance of a program and can have one or more threads. Threads within the same process share memory, while different processes have separate memory spaces.
- Concurrency (handling multiple things at once) is not the same as parallelism (doing multiple things at once). On a single-core CPU, threads are interleaved to give the illusion of parallelism, while on a multi-core CPU, they can run in true parallel.