Thread Priority in Java

Thread Priority in Java with Examples

In this article, I am going to discuss Thread Priority in Java with Examples. Please read our previous article where we discussed Thread Life Cycle in Java. At the end of this article, you will understand what is Thread Priority in Java and when and how to use Java Thread Priority with Examples.

Thread Priority in Java:

Priority means the number of resources allocated to a particular thread. Every thread created in JVM is assigned a priority. The priority range is between 1 and 10.

  1. 1 is called minimum priority
  2. 5 is called normal priority
  3. 10 is called maximum priority

The default priority of the main thread is 5, child thread will take the priority that is equal to its parent thread priority. We can change the priority of any thread whether it may be the main thread or a user-defined thread. It is recommended to change the priority by using constants available in the Thread class as follows:

  1. Thread.MIN_PRIORITY;
  2. Thread.NORM_PRIORITY;
  3. Thread.MAX_PRIORITY;

Threads are assigned priorities, based on which the thread scheduler can use to determine how the thread will be scheduled. The thread scheduler can use thread priorities to determine which thread gets to run.

The priority of a thread can be set by using the setPriority() and read using the getPriority() method, both are defined in the Thread class with the bellow prototype.

  1. Public final void setPriority(int newPriority)
  2. Public final int getPriority()

Note: The newPriority value range should be between 1 to 10 else it leads to exception java.lang.illegalArgumentException.

Example:
t.setPriority(7); //valid
t.setPriority(Thread.NORM_PRIORITY+2); //valid(recommended)

Note: According to sun microsystems JVM the range of thread priority is 1-10. The range of thread priorities will be changed from JVM to JVM. If the specified priority value is not in the range of priority then the setPriority() method throws a runtime exception called IllegalArgumentException.

Example to understand Java Thread Priority:
public class ThreadPriority extends Thread
{
    public void run ()
    {
        System.out.println ("running thread name is:" + Thread.currentThread ().getName ());
        System.out.println ("running thread priority is:" + Thread.currentThread ().getPriority ());
    }
    public static void main (String args[])
    {
        ThreadPriority m1 = new ThreadPriority ();
        ThreadPriority m2 = new ThreadPriority ();
        m1.setPriority (Thread.MIN_PRIORITY);
        m2.setPriority (Thread.MAX_PRIORITY);
        m1.start ();
        m2.start ();
    }
}
Output:

Program to understand Java Thread Priority

Thread Name:

The User-defined thread in Java is created with the default name “Thread-” +<index>, where the index is the integer numbers starting with 0. So the first user-defined thread name will be Thread-0, the second thread name is Thread-1, and so on.

The name of a thread can be set using the setNmae() method and read using the getName() method, both are defined in the Thread class with the bellow prototype.

  1. Public final void setName(String name)
  2. Public final String getName()

So the default thread name can be changed by using either

  1. At the time of thread object creation using string parameterized constructor or
  2. After object creation using a set statement
Example to Create Custom Thread with user-defined name and Priority in Java
public class MyThread extends Thread
{
    MyThread ()
    {
        super ();
    }
    MyThread (String name)
    {
        super (name);
    }
    public void run ()
    {
        for (int i = 0; i < 5; i++)
        {
            System.out.println (getName () + "i:" + i);
        }
    }
}

class ThreadNameAndPriority
{
    public static void main (String args[])
    {
        MyThread mt1 = new MyThread ();
        MyThread mt2 = new MyThread ("child2");

        System.out.println ("mt1 threads initial name and priority");
        System.out.println ("mt1 name:" + mt1.getName ());
        System.out.println ("mt1 priority:" + mt1.getPriority ());
        System.out.println ();

        System.out.println ("mt2 threads initial name and priority");
        System.out.println ("mt2 name:" + mt2.getName ());
        System.out.println ("mt2 priority:" + mt2.getPriority ());

        mt1.setName ("child1");

        mt1.setPriority (6);
        mt2.setPriority (9);

        System.out.println ("mt1 threads changed name and priority");
        System.out.println ("mt1 name:" + mt1.getName ());
        System.out.println ("mt1 priority:" + mt1.getPriority ());

        System.out.println ();

        System.out.println ("mt2 threads changed name and priority");
        System.out.println ("mt2 name:" + mt2.getName ());
        System.out.println ("mt2 priority:" + mt2.getPriority ());

        mt1.start ();
        mt2.start ();

        for (int i = 0; i < 5; i++)
        {
            System.out.println ("main i: " + i);
        }
    }
}
Output:

Example to Create Custom Thread with user-defined name and Priority

How to Retrieve the currently executing thread object Reference in Java?

The Bellow method is used to retrieve references of the currently executing thread object.

Public static native Thread curreantThread(): This method is useful to perform some operations on a thread object when its reference is not stored in our logic.

Example of Changing Main thread Name and Priority. Also, this example shows a static block is executed in the main thread.
public class CurrentThreadDemo
{
    static
    {
        System.out.println ("in static block");
        //retrieving currently executing thread reference
        Thread th = Thread.currentThread ();
        System.out.println ("SB is executing in" + th.getName () + "thread");
    }
    public static void main (String a[])
    {
        System.out.println ("in main method");
        //retrirving currently executing thread reference
        Thread th = Thread.currentThread ();
        System.out.println ("original name and priority of main thread");
        System.out.println ("current thread name:" + th.getName ());
        System.out.println ("current thread priority:" + th.getPriority ());

        th.setName ("xxyy");
        th.setPriority (7);

        System.out.println ("\nmodified name and priority of main thread");
        System.out.println ("modified thread name:" + th.getName ());
        System.out.println ("modified thread priority:" + th.getPriority ());
    }
}
Output:

How to Retrieve the currently executing thread object Reference

In the next article, I am going to discuss Daemon Threads in Java with Examples. Here, in this article, I try to explain Thread Priority in Java with examples. I hope you enjoy this Thread Priority in Java with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *