Java Thread Tutorial - Java Thread Priority

All threads have a priority.

The priority is indicated by an integer between 1 and 10.

A thread with the priority of 1 has the lowest priority. A thread with the priority of 10 has the highest priority.

There are three constants defined in the Thread class to represent three different thread priorities as listed in the following table.

Thread Priority Constants Integer Value

The thread with higher priority should have more the CPU time.

The priority of a thread is just a hint to the scheduler.

The setPriority() method of the Thread class sets a new priority for the thread.

The getPriority() method returns the current priority for a thread.

When a thread is created, its priority is set to the priority of the thread that creates it.


The following code demonstrates how to set and get the priority of a thread.

public class Main {
  public static void main(String[] args) {
    Thread t = Thread.currentThread();//from   ww w  .j  a  v  a  2 s  . c o  m
    System.out.println("main Thread  Priority:" + t.getPriority());

    Thread t1 = new Thread();
    System.out.println("Thread(t1) Priority:" + t1.getPriority());

    System.out.println("main Thread  Priority:" + t.getPriority());

    Thread t2 = new Thread();
    System.out.println("Thread(t2) Priority:" + t2.getPriority());

    // Change thread t2 priority to minimum
    System.out.println("Thread(t2) Priority:" + t2.getPriority());

The code above generates the following result.