JAVA - Zero to Hero / Multithreading

Thread Priorities

✅ What is Thread Priority?

  • Every thread in Java has a priority, which helps the CPU decide which thread to run first.

  • Priority is an integer value between 1 and 10.

  • Higher priority means the thread is more likely to be chosen by the CPU — but it's not guaranteed.


📊 Priority Levels

1️⃣ Thread.MIN_PRIORITY

  • It is a constant with value 1.

  • Represents the lowest possible priority.

  • Tells the JVM: "This thread is least important."

  • Other higher-priority threads will get preference over this.

  • Use it when the task is not urgent or can run in the background.


2️⃣ Thread.NORM_PRIORITY

  • It is a constant with value 5.

  • Represents the default (normal) priority for all threads.

  • If you don’t manually set any priority, the thread runs with this.

  • Balanced behavior — neither low nor high priority.


3️⃣ Thread.MAX_PRIORITY

  • It is a constant with value 10.

  • Represents the highest possible priority.

  • Tells the JVM: "This thread is very important."

  • JVM may give it more CPU time over other threads.

  • Use it when the task is critical or time-sensitive.

Constant

Value

Meaning

Thread.MIN_PRIORITY

1

Lowest priority

Thread.NORM_PRIORITY

5

Default priority

Thread.MAX_PRIORITY

10

Highest priority


🛠️ How to Set/Get Thread Priority

  • Use setPriority(int) to assign a thread's priority.

  • Use getPriority() to check it.

🧠 Example (just for understanding):

t1.setPriority(Thread.MAX_PRIORITY);   // Sets t1's priority to 10
int p = t1.getPriority();              // Gets the priority

⚠️ Important Notes

  • CPU may or may not follow priorities strictly.

  • Thread priority only gives a suggestion to the thread scheduler.

  • On some platforms, all threads might behave as if they have the same priority.


You can set more than one thread with the same priority — whether it's Thread.MAX_PRIORITY, Thread.MIN_PRIORITY, or any value between 1 and 10.

✅ What Happens If Multiple Threads Have the Same Priority?

If multiple threads have the same priority, the JVM thread scheduler will use thread scheduling policies (like round-robin or time slicing) to decide which thread runs next.

🧠 Key Points:

  • ✔️ You can set any number of threads to the same priority.

    Example:

    t1.setPriority(Thread.MAX_PRIORITY);
    t2.setPriority(Thread.MAX_PRIORITY);
  • ⚙️ If two or more threads have equal priority, the order of execution is not guaranteed.

  • 🕰️ The OS-level thread scheduler decides which thread gets CPU time — Java only suggests priority, it doesn't enforce it.

✅ Example Use Case

If two threads are ready to run:

  • The one with higher priority is more likely to get CPU time first.

  • But if the system is busy or platform doesn’t support it, both may run equally.


📝 Summary

Method

Use

setPriority(int)

Sets the thread's priority

getPriority()

Returns the thread's priority

Priority range

1 (low) to 10 (high)

Default value

5 (NORM_PRIORITY)