Login    New User    Forgot Password    Help      

Threrad Priorities

Java Tutorials

thread scheduler can use the threadpriorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.  

Priorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5(Thread.NORM_PRIORITY).  

 

 Constant  Description
 Thread.MIN_PRIORITY  The maximum priority of any thread (an int value of 10)
 Thread.MAX_PRIORITY  The minimum priority of any thread (an int value of 1)
 Thread.NORM_PRIORITY  The normal priority of any thread (an int value of 5)


 The methods that are used to set the priority of thread shown as:

 

 Method  Description

 setPriority() 

This is method is used to set the priority of thread.  
 getPriority()  This method is used to get the priority of thread.

 

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 newhigher 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.

Example

// Priorities
/*
volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program.
*/

class newtread implements Runnable
{
int counter=0;
Thread t;
private volatile boolean running = true;

       
        newtread(int p)
        {
        // creates other thread
        t= new Thread(this , "New tread");
        t.setPriority(p);
        }

        // entry point for the second thread
        public void run()
        {
        while(running)
                {      
                counter++;
                }
        }

        public void stop()
        {
        running=false;
        }

        public void start()
        {
        t.start();
        }

}

class tread5
{
public static void main(String args[])
{

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
// start multiple thread

newtread ob_high =new newtread(Thread.NORM_PRIORITY + 2);
newtread ob_low =new newtread(Thread.NORM_PRIORITY - 2 );

ob_low.start();
ob_high.start();

 

    try
        {
        Thread.sleep(1000);

        }
     catch(InterruptedException e)
        {
        System.out.println("Main thread interrupted ");
        }

 

ob_high.stop();
ob_low.stop();

// wait for child threads to terminate
try
        {
        ob_high.t.join();
        ob_low.t.join();
        }
catch(InterruptedException e)
        {
        System.out.println("Interrupted Exception caught ");
        }

System.out.println("Low priority thread :" + ob_low.counter);
System.out.println("High Priority thread  : "+ob_high.counter);

 

}
}

Output

Thread Priorities Summary

  • Most computers have only one CPU, thus threads must share the CPU with other threads. The execution of multiple threads on a single CPU, in some order, is called scheduling. The Java runtime supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling.
  • Each Java thread is given a numeric priority between MIN_PRIORITY and MAX_PRIORITY (constants defined in class Thread). At any given time, when multiple threads are ready to be executed, the thread with the highest priority will be chosen for execution. Only when that thread stops, or is suspended for some reason, will a lower priority thread start executing.
  • Scheduling of the CPU is fully preemptive. If a thread with a higher priority than the currently executing thread needs to execute, the higher priority thread is immediately scheduled.
  • The Java runtime will not preempt the currently running thread for another thread of the same priority. In other words, the Java runtime does not time-slice. However, the system implementation of threads underlying the Java Thread class may support time-slicing. Do not write code that relies on time-slicing.
  • In addition, a given thread may, at any time, give up its right to execute by calling the yield() method. Threads can only yield the CPU to other threads of the same priority--attempts to yield to a lower priority thread are ignored.
  • When all the "Runnable" threads in the system have the same priority, the scheduler chooses the next thread to run in a simple, non-preemptive, round-robin scheduling order

Java Tutorials