java tutorial - Thread priority in java - java programming - learn java - java basics - java for beginners



Thread priority in java

Learn Java - Java tutorial - Thread priority in java - Java examples - Java programs

  • Each thread has a priority.
  • The priorities are represented by a number among 1 and 10.
  • In the majority cases, thread scheduler schedules threads according to their priority (called as preemptive scheduling).
  • But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.

Constants of in Thread class

  • public static int MIN_PRIORITY public static int NORM_PRIORITY public static int MAX_PRIORITY
    • Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and then the value of MAX_PRIORITY is 10.

Sample Code

public class MultiPriority 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[]){
  MultiPriority p1=new MultiPriority();
  MultiPriority p2=new MultiPriority();
  p1.setPriority(Thread.MIN_PRIORITY);
  p2.setPriority(Thread.MAX_PRIORITY);
  p1.start();
  p2.start();
 
 }
}   
click below button to copy the code. By - java tutorial - team

Output

running thread name is:Thread-1
running thread name is:Thread-0
running thread priority is:10
running thread priority is:1

Related Searches to Thread priority in java