What are the Thread Priorities?

  • Every Thread in java has some priority it may be default priority generated by JVM (or) explicitly provided by the programmer.
  • The valid range of Thread priorities is 1 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

  • Thread scheduler uses these priorities while allocating CPU.
  • The Thread which is having highest priority will get chance for 1st
  • If 2 Threads having the same priority then we can’t expect exact execution order it depends on Thread scheduler whose behavior is vendor dependent.
  • We can get and set the priority of a Thread by using the following methods.

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”.
  • 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.

Leave a Reply

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