Pages

Sunday 27 October 2013

Set and get the Thread Priorities in Jav



          The thread priorities are the real numbers used by thread scheduler to decide when each thread should be allowed to execute. The range of thread priority is within the MIN_PRIORITY and MAX_PRIORITY. The default value or normal priority is NORM_PRIORITY. The below table list the values of the maximum, minimum and default priorities in Java.
            
Keyword
Real Number Representation
MIN_PRIORITY
1
MAX_PRIORITY
10
NORM_PRIORITY
5
        
          The default priority of any thread in Java will be NORM_PRIORITY i.e. the value 5. You can set the priorities of any thread to different desired values within the range by using the method setPriority() of Thread. The general form of this method is shown below.

final void setPriority ( int level);


          The value of the current priority of any thread can be obtained by using the getPriority() method of Thread. The general form of this method is shown below.

final int getPriority (); 


          The below example program shows how to set and get the thread priorities.

package blog.exception.mainThread;

public class MyThreadPriority implements Runnable
{
  String ThreadName;
  int priorityValue;
  Thread th;
  
  public MyThreadPriority(String ThreadName,int priorityValue)
  {
                this.ThreadName=ThreadName;
                this.priorityValue=priorityValue;
                th=new Thread(this,this.ThreadName);
               
                th.setPriority(this.priorityValue);// setting thread priority
                System.out.println();
                System.out.println("Thread Priority of "+this.ThreadName+" is "+th.getPriority());//getting thread priority using method.
                th.start();
  }
  // entry point for the thread.
 
  @Override
                public void run()
    {
                                try
                                {
                                                for(int i=5;i>0;i--)
                                                {
                                                                System.out.println(ThreadName+ ":"+ i);
                                                                Thread.sleep(500);
                                                }
                                }
                                catch(Exception ex)
                                {
                                                ex.printStackTrace();
                                }
                  System.out.println("Am exiting :"+ThreadName);         
                }
 
}

package blog.exception.mainThread;

public class DemoThreadPriority
{
   public static void main(String[] args)
   {
                new MyThreadPriority("Child1", 10);
                new MyThreadPriority("Child2",1);
                new MyThreadPriority("Child3", 5);
  
  
     try
     {
                   //main thread
                   Thread.sleep(10000);
     }
     catch (Exception ex)
     {
                 ex.printStackTrace();
     }
    
     System.out.println("Main thread exiting");
   }
}


Output:

Thread Priority of Child1 is 10

Thread Priority of Child2 is 1
Child1:5

Thread Priority of Child3 is 5
Child2:5
Child3:5
Child3:4
Child2:4
Child1:4
Child1:3
Child3:3
Child2:3
Child3:2
Child1:2
Child2:2
Child3:1
Child2:1
Child1:1
Am exiting :Child1
Am exiting :Child2
Am exiting :Child3
Main thread exiting

           The thread priorities are used to decide when to switch from one running thread to the next thread. This switching is called context switching.