Latest News

Thursday, September 29, 2016

Thread Priroty in java.

Thread Priority:

  1. public static int MIN_PRIORITY=1
  2. public static int NORM_PRIORITY=5
  3. public static int MAX_PRIORITY=10

Example:

 class Test implements Runnable{
    public void run(){
        System.out.println("Priority of current thread="+Thread.currentThread().getPriority());
   //getting default priority
    }
  
}
public class GetPriorityDemo{
    public static void main(String[] args) {
        Test t=new Test();
        Thread t1=new Thread(t);
        t1.start();
    }
}


Set Priority in java: 

class Test1 implements Runnable{
    public void run(){
        System.out.println("Priority of current thread="+Thread.currentThread().getPriority());
    }
   
}
public class SetPriorityDemo{
    public static void main(String[] args) {
        Test t=new Test();
        Thread t1=new Thread(t);
        t1.setPriority(4);//set priority
        t1.start();
        //Thread.yield();
    }
}