Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Thread Priorities

Thread scheduler uses priority of threads to decide which thread has right to execute first before other threads.

The thread which has the highest priority will execute first. And the threads with minimum priority will execute later.

In Java, the thread can contain priorities from 1 to 10.

1 is the minimum priority, 10 is a maximum priority, and default priority is 5.

Therefore there are three constant priorities defined in java,
1) MIN_PRIORITY – 1
2) NORM_PRIORITY – 5
3) MAX_PRIORITY – 10

If there are multiple threads with same priority waiting for execution, the result can’t be predicted because it completely depends on thread scheduler to decide which thread should be executed first.
It depends on the algorithm followed by thread scheduler.

Check Priority

Thread class contains the following method to check priority.

public final int getPriority();

Example

class CheckPriority extends Thread
{
    public void run()
    {
        System.out.println("Child Thread Priority :: "+
                                      Thread.currentThread().getPriority());
    }
}

class PriorityDemo
{
    public static void main(String args[])
    {
        CheckPriority c = new CheckPriority();
        c.start();
        System.out.println("Main Thread Priority :: "+
                                       Thread.currentThread().getPriority());
    }
}

Output

Main Thread Priority :: 5
Child Thread Priority :: 5

                                                                                  OR

Child Thread Priority :: 5
Main Thread Priority :: 5

Set Priority

We can not only get a priority of thread but can also set a priority to thread explicitly.
Thread class contains the following method to set priority.

public final void setPriority(int priorityLevel)

Note

Priority must be set only within 1 to 10. Otherwise, we will get exception,
                       java.lang.IllegalArgumentException.

Example

class SetPriority extends Thread
{
    public void run()
    {
         System.out.println("Default Priority of Child thread :: "+
                                        Thread.currentThread().getPriority());
         Thread.currentThread().setPriority(9);
         System.out.println("New Priority of Child thread :: "+
                                         Thread.currentThread().getPriority());
    }
}

class PriorityDemo
{
    public static void main(String args[])
    {
        SetPriority s = new SetPriority();
        s.start();
        System.out.println("Main Thread Priority :: "+
                                         Thread.currentThread().getPriority());
    }
}

Output

Main Thread Priority :: 5
Default Priority of Child thread :: 5
New Priority of Child thread :: 9

The default priority of Child thread is 5 because it inherits from the main thread. And the new priority of Child thread is 9 because it is set explicitly by using setPriority(9).

 

Leave a reply