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.

  • High-priority threads might execute before low-priority threads, but not always guaranteed.

  • OS-dependent behavior: Some JVMs may completely ignore priority settings.

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

Priority Level

Effect

Low (1-4)

Less CPU time, runs after high-priority threads.

Normal (5-9)

Balanced execution.

High (10)

May get more CPU time, but not guaranteed.

Setting Thread Priority

We can set a thread's priority using:

And retrieve it using:

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.

Output (default JVM behavior):

Setting and Getting Thread Priority

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

Priority May Not Always Work

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

Expected vs. Actual Output

Threads do not always run in expected order.

Usage of Thread Priorities

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

Last updated