A thread is the smallest unit of execution within a process.
• It represents a single path of execution within a process, containing its own program counter, register set, and stack, but sharing the process’s resources such as memory and file descriptors with other threads in the same process.
Key Characteristics of Threads:
- Concurrency: Threads allow a process to perform multiple tasks concurrently, enhancing the application’s performance and responsiveness.
- Shared Resources: Threads within the same process share the same memory space, which allows for easier communication and data sharing among threads.
- Lightweight: Compared to processes, threads are lightweight because they do not require separate memory allocation for each thread.
Types of Threads:
- User-Level Threads: Managed by a user-level library without kernel support. They are faster to create and manage but lack true parallelism because the kernel does not recognize them.
- Kernel-Level Threads: Managed directly by the operating system kernel. They support true parallelism and can be scheduled independently by the OS.
- Hybrid Threads: A combination of user-level and kernel-level threads, aiming to balance performance and functionality.
Thread Lifecycle
Threads undergo various states during their execution. The typical states include:
- New: The thread is being created.
- Runnable: The thread is ready to run and waiting for CPU time.
- Running: The thread is currently executing on the CPU.
- Waiting: The thread is waiting for another thread to perform a specific action.
- Timed Waiting: The thread is waiting for a specified amount of time.
- Terminated: The thread has finished its execution.