Skip to content

Thread priority

Thread priority in Java is a mechanism that helps the operating system determine the order in which threads are scheduled for execution. Every thread in Java is assigned a priority, which is an integer value ranging from 1 (the minimum) to 10 (the maximum). Thread priorities are defined using constants in the Thread class:

  • Thread.MIN_PRIORITY = 1
  • Thread.NORM_PRIORITY = 5 (default priority)
  • Thread.MAX_PRIORITY = 10

Key Concepts

  1. Priority Definition: Thread priority is used by the thread scheduler to decide which thread should run when multiple threads are in a ready-to-run state. The thread with a higher priority is likely to be executed first, but thread priority does not guarantee execution order.
  2. Setting and Getting Priority:
    1. Use setPriority(int priority) to assign a priority to a thread.
    1. Use getPriority() to retrieve the current priority of a thread.

Thread t = new Thread();

t.setPriority(Thread.MAX_PRIORITY); // Set the priority to 10

System.out.println(“Thread Priority: ” + t.getPriority());

  • Default Priority: By default, a thread inherits the priority of the thread that created it. If no specific priority is assigned, it defaults to Thread.NORM_PRIORITY.
  • Impact of Priority:
    • Thread priorities influence thread scheduling. However, thread scheduling behavior is platform-dependent.
    • In preemptive scheduling systems, threads with higher priority are more likely to be executed.
    • In time-slicing systems, threads of equal priority share the CPU time equally.
  • Priority Ranges:
    • Threads should be assigned priority values based on their importance relative to other threads in the application.
    • Avoid overusing maximum priority as it can lead to performance issues, such as starving lower-priority threads.

Example of Thread Priority

class MyThread extends Thread {

    public void run() {

        System.out.println(Thread.currentThread().getName() + ” – Priority: ” + Thread.currentThread().getPriority());

    }

}

public class ThreadPriorityExample {

    public static void main(String[] args) {

        MyThread t1 = new MyThread();

        MyThread t2 = new MyThread();

        MyThread t3 = new MyThread();

        t1.setPriority(Thread.MIN_PRIORITY); // Priority 1

        t2.setPriority(Thread.NORM_PRIORITY); // Priority 5

        t3.setPriority(Thread.MAX_PRIORITY); // Priority 10

        t1.start();

        t2.start();

        t3.start();

    }

}

Output (Example):

Thread-0 – Priority: 1

Thread-1 – Priority: 5

Thread-2 – Priority: 10

Note: The actual execution order may vary depending on the JVM and OS.

Limitations

  • Thread priority is not a reliable mechanism for thread synchronization.
  • It is not guaranteed that a higher-priority thread will always execute before a lower-priority thread because thread scheduling is managed by the operating system.

Best Practices

  • Use priorities judiciously to influence scheduling when necessary.
  • Rely on proper thread synchronization mechanisms (e.g., wait(), notify(), synchronized) for thread coordination rather than relying solely on priorities.