VOOZH about

URL: https://www.geeksforgeeks.org/java/java-thread-priority-multithreading/

⇱ Java Thread Priority in Multithreading - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Thread Priority in Multithreading

Last Updated : 24 Apr, 2026

Java supports multithreading, where multiple threads run concurrently and the Thread Scheduler decides their execution order. Each thread is assigned a priority (1–10) that influences scheduling but does not guarantee execution order.

  • Changing priority does not guarantee faster execution or immediate scheduling
  • Higher-priority threads are generally preferred by the scheduler
  • Actual execution order depends on the JVM and underlying OS

Output
LowPriorityThread is running with priority 1
HighPriorityThread is running with priority 10

Explanation:

  • Two threads are created: HighPriorityThread and LowPriorityThread.
  • HighPriorityThread is assigned maximum priority (10), and LowPriorityThread gets minimum priority (1).
  • When start() is called, both threads run concurrently.
  • Higher-priority threads may get more CPU time, but the actual execution order is not guaranteed because it depends on the JVM and the OS scheduler.

Thread Priority Constants

👁 java_thread_priority

Java provides three constant values in the Thread class:

  • Thread.MIN_PRIORITY (1): Lowest possible priority for a thread.
  • Thread.NORM_PRIORITY (5): Default priority assigned to a thread.
  • Thread.MAX_PRIORITY (10): Highest possible priority for a thread.

Setting and Getting Thread Priority

We can use the following methods of the Thread class to manage thread priority:

  • setPriority(int newPriority): This method is used to set the priority of a thread.
  • getPriority(): This method is used to returns the current priority of the thread.

Output
Thread-2 with priority 5
Thread-1 with priority 1
Thread-3 with priority 10

Explanation: The program creates three threads, assigns them different priorities (1, 5, and 10), and starts them using start(). Each thread runs concurrently and prints its name and priority, but the execution order is not fixed and depends on the scheduler.

Note: The output order may vary because thread scheduling depends on the JVM and the underlying operating system.

If multiple threads have the same priority, their execution order is decided by the thread scheduler. The example below demonstrates this, followed by an explanation of the output for better conceptual and practical understanding.


Output
t1 priority: 5
t2 priority: 5
Thread-1 is running with priority 5
Thread-0 is running with priority 5

Explanation: The main thread sets its priority to 5, and both t1 and t2 inherit this priority when created. When start() is called, both threads run concurrently and print their details, and since they have the same priority, their execution order depends on the OS scheduler.

Comment
Article Tags: