> 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/thread-priority.md).

# Thread Priority

## About

Thread priority in Java determines the order in which threads are scheduled for execution. However, it **does not guarantee exact execution sequence**—it is just a hint to the thread scheduler.

{% hint style="info" %}

* **High-priority threads** might execute **before low-priority threads**, but **not always guaranteed**.
* **OS-dependent behavior**: Some JVMs may completely ignore priority settings.
  {% endhint %}

## **Thread Priority Levels**

Java defines **three standard priority constants** in the `Thread` class:

| **Constant**           | **Value** | **Description**  |
| ---------------------- | --------- | ---------------- |
| `Thread.MIN_PRIORITY`  | `1`       | Lowest priority  |
| `Thread.NORM_PRIORITY` | `5`       | Default priority |
| `Thread.MAX_PRIORITY`  | `10`      | Highest priority |

## **Thread Priority Effects**

<table data-header-hidden><thead><tr><th width="229"></th><th></th></tr></thead><tbody><tr><td><strong>Priority Level</strong></td><td><strong>Effect</strong></td></tr><tr><td><strong>Low (1-4)</strong></td><td>Less CPU time, runs after high-priority threads.</td></tr><tr><td><strong>Normal (5-9)</strong></td><td>Balanced execution.</td></tr><tr><td><strong>High (10)</strong></td><td>May get more CPU time, but not guaranteed.</td></tr></tbody></table>

## **Setting Thread Priority**

We can set a thread's priority using:

```java
thread.setPriority(somePriorityValue);
```

And retrieve it using:

```java
int p = thread.getPriority();
```

## **Default Thread Priority**

* The **main thread** has a default priority of `5` (`NORM_PRIORITY`).
* A newly created thread **inherits the priority** of the thread that created it.

```java
public class DefaultPriorityDemo {
    public static void main(String[] args) {
        System.out.println("Main thread priority: " + Thread.currentThread().getPriority());

        Thread t = new Thread(() -> System.out.println("New thread priority: " + Thread.currentThread().getPriority()));
        t.start();
    }
}
```

**Output (default JVM behavior):**

```
Main thread priority: 5
New thread priority: 5
```

## **Setting and Getting Thread Priority**

```java
class PriorityDemo extends Thread {
    public void run() {
        System.out.println("Thread Name: " + Thread.currentThread().getName() +
                ", Priority: " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        PriorityDemo t1 = new PriorityDemo();
        PriorityDemo t2 = new PriorityDemo();
        PriorityDemo t3 = new PriorityDemo();

        t1.setPriority(Thread.MIN_PRIORITY);  // 1
        t2.setPriority(Thread.NORM_PRIORITY); // 5 (default)
        t3.setPriority(Thread.MAX_PRIORITY);  // 10

        t1.start();
        t2.start();
        t3.start();
    }
}
```

**Possible Output (varies based on OS & JVM scheduler):**

```
Thread Name: Thread-2, Priority: 10
Thread Name: Thread-1, Priority: 5
Thread Name: Thread-0, Priority: 1
```

## **Priority May Not Always Work**

Even if a thread has `MAX_PRIORITY`, the JVM **does not guarantee** execution order.

```java
for (int i = 0; i < 5; i++) {
    Thread high = new Thread(() -> System.out.println("High Priority"));
    Thread low = new Thread(() -> System.out.println("Low Priority"));

    high.setPriority(Thread.MAX_PRIORITY);
    low.setPriority(Thread.MIN_PRIORITY);

    low.start();
    high.start();
}
```

**Expected vs. Actual Output**

```
High Priority
Low Priority
Low Priority
High Priority
High Priority
Low Priority
Low Priority
High Priority
```

Threads do **not always** run in expected order.

## Usage of Thread Priorities

A payment processing thread gets higher priority than a logging thread.


---

# 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/thread-priority.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.
