4) Multithreading Lesson

Java Thread Priority

5 min to complete · By Ryan Desmond

In Java, not all threads are equal; at least, they don't have to be. You can set a thread's priority to a value between 1 (low) and 10 (high).

How to Set a Thread Priority?

Every Thread in Java is automatically set with a priority, which allows the JVM to apply a preference to threads with higher priority if computational power is low.

You can also use the built-in static final variables Thread.MIN_PRIORITY (1), Thread.NORM_PRIORITY (5), and Thread.MAX_PRIORITY (10). The setPriority() method takes in an int as a parameter. Thread.MIN_PRIORITY, for example, is an int variable in the Thread class. Due to this, you can add or subtract from it, i.e., Thread.NORM_PRIORITY+2.

Setting Thread Priority in Java

Below are multiple examples of how to set the priority of a Thread in Java.

/* set current thread to the highest priority. */
Thread
  .currentThread()
  .setPriority(Thread.MAX_PRIORITY);

/* or */
Thread
  .currentThread()
  .setPriority(1);

/* or */
Thread
  .currentThread()
  .setPriority(7);

/* or */
Thread
  .currentThread()
  .setPriority(Thread.NORM_PRIORITY+2);

Why Change a Thread's Priority?

Answer me this: should the process that monitors and updates the movement of your computer's mouse ever have to wait for CPU time behind a background process that is deleting old files? Answer: No - It shouldn't. Some processes are more (immediately) important than others. For this reason, you have the ability to dictate a Threads priority.

Example of Setting a Thread Priority

// This is a class that implements Runnable - it's a thread
class Priority implements Runnable {

  int count;
  /* create the Thread object that will manage the thread */
  Thread thrd;
  static boolean stop = false;
  static String currentName;
  int id = 0;

  /* Construct a new thread. Notice that this
     constructor does not actually start the
     threads running. */
  Priority(String name) {
    thrd = new Thread(this, name);
    count = 0;
    currentName = name;

    /* Notice that you do not start the thread in the 
       constructor this time. The choice is yours. If you do 
       not start the thread (thrd.start()) here in the constructor, 
       then you must manually start it from the controlling class. 
       See "mt1.thrd.start();" in the PriorityDemo class below */
  }

  /* Begin execution of new thread. */
  public void run() {
    System.out.println(thrd.getName() + " starting.");

    do {
      count++;
      if(currentName.compareTo(thrd.getName()) != 0) {
        currentName = thrd.getName();
        System.out.println("In " + currentName);
      }
    } while(stop == false && count < 10000000);
        
    stop = true; 
    System.out.println("\n" 
      + thrd.getName() + " terminating.");
  }
}

class PriorityDemo {
  public static void main(String args[]) {
    Priority mt1 = new Priority("High Priority");
    Priority mt2 = new Priority("Low Priority");

    /* set the priorities */
    mt1.thrd.setPriority(Thread.NORM_PRIORITY+2);
    mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);

    /* start the threads */
    mt1.thrd.start();
    mt2.thrd.start();

    try {
      mt1.thrd.join();
      mt2.thrd.join();
    } catch(InterruptedException exc) {
      System.out.println("Main thread interrupted.");
    }

    System.out.println(
      "\nHigh priority thread counted to " + mt1.count);
    System.out.println(
      "Low priority thread counted to " + mt2.count);
  }
}

Summary: How to Prioritize Threads

  • Every Thread automatically receives a priority in Java
  • Priorities range from 1 (low) to 10 (high)
  • The JVM will preference higher priority threads if computational power is low
  • Java comes with three static final variables for setting priorities
    • Thread.MIN_PRIORITY equals 1
    • Thread.NORM_PRIORITY equals 5
    • Thread.MAX_PRIORITY equals 10
  • The setPriority() method is used to manually set the priority