fastread homefastrread library fastread menu

Java : Priority of a Thread (Thread Priority)

Tutorial by:Arena Buocel      Date: 2016-05-04 05:15:47

❰ Previous Next ❱

Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.

3 constants defiend in Thread class:

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

Example of priority of a Thread:

 
  1. class TestMultiPriority1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("running thread name is:"+Thread.currentThread().getName());  
  4.    System.out.println("running thread priority is:"+Thread.currentThread().getPriority());  
  5.   
  6.   }  
  7.  public static void main(String args[]){  
  8.   TestMultiPriority1 m1=new TestMultiPriority1();  
  9.   TestMultiPriority1 m2=new TestMultiPriority1();  
  10.   m1.setPriority(Thread.MIN_PRIORITY);  
  11.   m2.setPriority(Thread.MAX_PRIORITY);  
  12.   m1.start();  
  13.   m2.start();  
  14.    
  15.  }  
  16. }  
Output:running thread name is:Thread-0
       running thread priority is:10
       running thread name is:Thread-1
       running thread priority is:1

❰ Previous Next ❱


Java

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website