Thread Architecture
Understand Java threads and how the JVM schedules them on the host OS.
Java Threads
Java threads are lightweight execution units managed by the JVM and underlying OS.
Each thread gets its own stack and execution context, making concurrency possible in Java applications.
Thread Lifecycle
A Java thread moves through states such as new, runnable, blocked, waiting, timed waiting, and terminated.
The lifecycle model is central to understanding application concurrency and thread debugging.
Thread Scheduling
Thread scheduling is handled by the operating system or a task scheduler, depending on the environment and JVM configuration.
It determines when each thread gets CPU time and how contention is managed.
Thread Priorities
Thread priorities hint the scheduler about the importance of a thread, but they are not strict guarantees.
Real scheduling behavior depends on the OS and platform, so priorities should not be relied on for correctness.
Daemon Threads
Daemon threads run in the background and do not prevent the JVM from exiting when only daemon threads remain.
They are suitable for housekeeping services such as monitoring or cleanup tasks.
User Threads
User threads keep the JVM alive while they are running. If a non-daemon thread is still active, the application continues.
This distinction is vital when designing background services and worker pools.
Thread Context Switching
Context switching occurs when the scheduler pauses one thread and resumes another to share CPU time.
Excessive context switching can hurt performance and increase lock contention or latency.