Java Thread Priority


In Java language, Each thread has a priority which schedules the thread with the help of the operating system.


Java thread priorities are within the range MIN_PRIORITY and MAX_PRIORITY. These values are 1 and 10, respectively. By default, every thread is given NORM_PRIORITY, which is currently 5.


  • public static int MIN_PRIORITY
  • public static int NORM_PRIORITY
  • public static int MAX_PRIORITY
Java Thread Priority

Syntax:

final void setPriority(int level)
final int getPriority( ) 

Example:

 public class ThreadPriority extends Thread{  
    public void run(){  
        System.out.println("Thread name is "+Thread.currentThread().getName());  
        System.out.println("Thread priority is "+Thread.currentThread().getPriority());  
    }  
    public static void main(String args[]){  
        ThreadPriority t1=new ThreadPriority();  
        ThreadPriority t2=new ThreadPriority();  
        ThreadPriority t3=new ThreadPriority();
        t1.setPriority(Thread.MIN_PRIORITY);  
        t2.setPriority(Thread.MAX_PRIORITY);  
        t3.setPriority(Thread.NORM_PRIORITY); 
        t1.start();  
        t2.start();  
        t3.start();
   
    }  
}



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.