Thread Priorities

In Java, thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities

Thread Priorities

In Java, thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities

Thread Priorities

Thread Priorities

     

 

In Java, thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.  

Priorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5(Thread.NORM_PRIORITY).  

 

 Constant  Description
 Thread.MIN_PRIORITY  The maximum priority of any thread (an int value of 10)
 Thread.MAX_PRIORITY  The minimum priority of any thread (an int value of 1)
 Thread.NORM_PRIORITY  The normal priority of any thread (an int value of 5)


 The methods that are used to set the priority of thread shown as:

 

 Method  Description

 setPriority() 

This is method is used to set the priority of thread.  
 getPriority()  This method is used to get the priority of thread.

 

When a Java thread is created, it inherits its priority from the thread that created it.  At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution. In Java runtime system, preemptive scheduling algorithm is applied. If at the execution time a thread with a higher priority and all other threads are runnable then the runtime system chooses the new higher priority thread for execution. On the other hand, if two threads of the same priority are waiting  to be executed by the CPU then the round-robin algorithm is applied in which the scheduler chooses one of them to run according to their round of time-slice.

Thread Scheduler

In the implementation of threading scheduler usually applies one of the two following strategies:

  • Preemptive scheduling ? If the new thread is a higher priority thread then current running thread moves to runnable state and higher priority thread start executing.
     

  • Time-Sliced (Round-Robin) Scheduling ? A running thread is allowed to execute for fixed length of time, after completion this fixed time the current thread sends to runnable state.

You can also set a thread's priority at any time after its creation using the setPriority method. Lets see, how to set and get the priority of a thread.

 

class MyThread1 extends Thread{
  MyThread1(String s){
    super(s);
    start();
  }
  public void run(){
    for(int i=0;i<3;i++){
      Thread cur=Thread.currentThread();
      cur.setPriority(Thread.MIN_PRIORITY);
      int p=cur.getPriority();
      System.out.println("Thread Name  :"+Thread.currentThread().getName());
      System.out.println("Thread Priority  :"+cur);
      }
  }
}
  class MyThread2 extends Thread{
  MyThread2(String s){
    super(s);
    start();
  }

public void run(){
    for(int i=0;i<3;i++){
      Thread cur=Thread.currentThread();
      cur.setPriority(Thread.MAX_PRIORITY);
      int p=cur.getPriority();
      System.out.println("Thread Name  :"+Thread.currentThread().getName());
      System.out.println("Thread Priority  :"+cur);
      }
  }
}
public class ThreadPriority{
  public static void main(String args[]){  
    MyThread1 m1=new MyThread1("My Thread 1");
    MyThread2 m2=new MyThread2("My Thread 2");
  }
}

 

Output of the Program:

 

C:\nisha>javac ThreadPriority.java

C:\nisha>java ThreadPriority
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Priority :Thread[My Thread 2,10,main]
Thread Name :My Thread 2
Thread Priority :Thread[My Thread 2,10,main]
Thread Name :My Thread 2
Thread Priority :Thread[My Thread 2,10,main]
Thread Priority :Thread[My Thread 1,1,main]
Thread Name :My Thread 1
Thread Priority :Thread[My Thread 1,1,main]
Thread Name :My Thread 1
Thread Priority :Thread[My Thread 1,1,main]

 

In this program two threads are created. We have set up maximum priority for the first thread "MyThread2" and minimum priority for the first thread "MyThread1" i.e. the after executing the program, the first thread is executed only once and the second thread "MyThread2" started to run until either it gets end or another thread of the equal priority gets ready to run state.

  

Download this Program