Friday, 25 November 2011

Thread priority


Priority is used as a guide for the operating system to determine which thread gets accesses to a resource such as the CPU. In reality, an operating system takes other factors into consideration. Typically, programmers have little or no control over those other factors. Therefore, they establish a priority for their threads without further concern over those other factors.

When a Java thread is created, it inherits its priority from the thread that created it.  At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution. In Java runtime system, preemptive scheduling algorithm is applied. If at the execution time a thread with a higher priority and all other threads are runnable then the runtime system chooses the new higher priority thread for execution. On the other hand, if two threads of the same priority are waiting  to be executed by the CPU then the round-robin algorithm is applied in which the scheduler chooses one of them to run according to their round of time-slice.

Thread Scheduler
In the implementation of threading scheduler usually applies one of the two following strategies:

Preemptive scheduling : If the new thread has a higher priority then current running thread leaves the runnable state and higher priority thread enter to the runnable state.
 
Time-Sliced (Round-Robin) Scheduling : A running thread is allowed to be execute for the fixed time, after completion the time, current thread indicates to the another thread to enter it in the runnable state.

A priority is an integer from 1 to 10 inclusive, where 10 is the highest priority, referred to as the maximum priority, and 1 is the lowest priority, also known as the minimum priority. The normal priority is 5, which is the default priority for each thread.

setPriority():

To set the priority of thread setPriority() method is used. Itis the member of Thread class.

Syntax :
 final void setPriority(int level)

Here, level specifies the new priority setting for the calling thread. The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY. Currently, these values are 1 and 10, respectively.
The thread class defines several priority constant.

MIN_PRIORITY=1
NORM_PRIORITY=5
MAX_PRIORITY=10

getPrority()

To get the current priority setting of thread  getPriority( ) method is used
Syntax :
final int getPriority( )
         
Here , it returns the priority of calling thread.

Ex,
class A extends Thread
{
   public void run()
    {
        System.out.println("Thread  A Started");
        for(int i=1;i<=5;i++)
         {
            System.out.println("Thread A -> No = " +i);
         }
      System.out.println("Exit from Thread A ");
   }
}

class B extends Thread
{
   public void run()
    {
        System.out.println("Thread  B Started");
        for(int i=1;i<=5;i++)
         {
            System.out.println("Thread B -> No = " +i);
         }
      System.out.println("Exit from Thread B ");
   }
}

class C extends Thread
{
   public void run()
    {
        System.out.println("Thread  C Started");
        for(int i=1;i<=5;i++)
         {
            System.out.println("Thread C -> No = " +i);
         }
      System.out.println("Exit from Thread C ");
   }
}

class ThreadPriority
{
public static void main(String args[])
{
  A obja=new A();
  B objb=new B();
  C objc=new C();

 objc.setPriority(Thread.MAX_PRIORITY);
 objb.setPriority(obja.getPriority()+1);
 obja.setPriority(Thread.MIN_PRIORITY);
  
System.out.println("start thread A");
obja.start(); 
System.out.println("start thread B");
objb.start();
System.out.println("start thread C");
objc.start();
 }
}

Posted by : Ruchita Pandya

No comments:

Post a Comment