> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/parallelism-and-concurrency/thread-fundamentals.md).

# Thread Fundamentals

A **thread** is the smallest unit of execution in a program. In Java, every program runs on at least one thread, known as the **main thread**.

## **1. What is a Thread?**

* A **thread** is a lightweight process that runs independently.
* Java supports **multithreading**, meaning multiple threads can run simultaneously.
* Each thread has its own **execution path** but shares resources with other threads in the same process.

### **Analogy**

Imagine a **thread** as a train running on a track.

* **A single-threaded program** is like one train running on a single track, completing one task at a time.
* **A multi-threaded program** is like multiple trains running on parallel tracks, performing tasks simultaneously.

Here’s a visual representation of threads:

```
Main Thread  -----> Task 1 -----> Task 2 -----> Task 3  (Sequential Execution)

Multi-Threading:
Thread 1  -----> Task A -----> Task B  
Thread 2  -----> Task X -----> Task Y  
(Thread 1 and Thread 2 run in parallel)
```

### **Java Thread Package**

Threads in Java are part of the `java.lang` package, primarily using the following classes and interfaces:

* `java.lang.Thread` → The main class for creating and managing threads.
* `java.lang.Runnable` → Functional interface for defining thread tasks.
* `java.util.concurrent` package → Provides higher-level concurrency utilities.

## **2. Why Use Threads?**

* Improves application performance by utilizing **multiple CPU cores**.
* Allows multiple tasks to execute **simultaneously** (e.g., UI responsiveness, background tasks).
* Enables **parallel execution** for tasks like data processing, computations, and network requests.

## **3. Thread Lifecycle**

A thread goes through multiple states:

1. **New** → Thread is created but not started.
2. **Runnable** → Ready to run but waiting for CPU.
3. **Running** → Actively executing.
4. **Blocked/Waiting** → Paused, waiting for a resource.
5. **Terminated** → Execution is completed or stopped.

## **4. Thread Priorities**

Each thread in Java has a priority (range **1 to 10**). Default priority is `5 (NORM_PRIORITY)`.

* `Thread.MIN_PRIORITY` → 1
* `Thread.NORM_PRIORITY` → 5
* `Thread.MAX_PRIORITY` → 10

## **5. Daemon vs User Threads**

* **User Threads** → Standard application threads.
* **Daemon Threads** → Background threads that terminate when no user thread is running.

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/parallelism-and-concurrency/thread-fundamentals.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
