Saturday 29 June 2019

Thread Priorities

  • Every Thread in java has some priority. It may be default priority generated by JVM (or) customized priority provided by the programmer.
  • The valid range of Thread priorities is 1 to 10[but not 0 to 10] where 1 is the least priority and 10 is highest priority.
  • Thread class defines the following constants to represent some standard priorities. 
                  1. Thread. MIN_PRIORITY    1
                  2. Thread. MAX_PRIORITY    10
                  3. Thread. NORM_PRIORITY    5
  • There are no constants like Thread.LOW_PRIORITY, Thread.HIGH_PRIORITY
  • Thread scheduler uses these priorities while allocating CPU(Processor).
  • The Thread which is having highest priority will get chance for first execution.
  • If 2 Threads having the same priority then we can't expect exact execution order it depends on Thread scheduler.
  • Thread class defines the following methods to get and set the priority of a Thread.
             1. public final int getPriority()
             2. public final void setPriority(int newPriority);//the allowed values are 1 to 10
  • The allowed values are 1 to 10 otherwise we will get runtime exception saying "IllegalArgumentException".
Example:
t.setPriority(8);//valid
t.setPriority(18);//IllegalArgumentException

Default priority:
  • The default priority only for the main Thread is 5. But for all the remaining Threads the default priority will be inheriting from parent to child. That is whatever the priority parent has by default the same priority will be for the child also.
Example 1:
class MyThread extends Thread
{}
class ThreadPriorityDemo
{
              public static void main(String[] args)
              {
                             System.out.println(Thread.currentThread().getPriority());//5
                             Thread.currentThread().setPriority(9);
                             MyThread t=new MyThread();
                             System.out.println(t.getPriority());//9
              }
}
 

 Example 2:
class MyThread extends Thread
{
              public void run()
              {
                             for(int i=0;i<10;i++)
                             {
                                           System.out.println("child thread");
                             }
              }
}
class ThreadPriorityDemo
{
              public static void main(String[] args)
              {
                             MyThread t=new MyThread();
                             //t.setPriority(10);          //----> 1 t.start();
                             for(int i=0;i<10;i++)
                             {
                                           System.out.println("main thread");
                             }
              }
}
 
  • If we are commenting line 1 then both main and child Threads will have the same priority and hence we can't expect exact execution order.
  • If we are not commenting line 1 then child Thread has the priority 10 and main Thread has the priority 5 hence child Thread will get chance for execution and after completing child Thread main Thread will get the chance in this the output is:
 Output:child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
  • Some operating systems(like windowsXP) may not provide proper support for Thread priorities. We have to install separate bats provided by vendor to provide support for priorities.

Thanks..!!

No comments:

Post a Comment