Saturday, 19 November 2016

What is Java Thread Priority ?

Previously we have discussed about

Thread Priority

In java each thread is assigned a priority, which affects the order in which it is scheduled for running. Thread priorities are integers that specify the relative priority of one thread to another. As an absolute value, a priority is meaningless; a higher-priority thread doesn’t run any faster than a lower-priority thread if it is the only thread running. Instead, a thread’s priority is used to decide when to switch from one running thread to the next. This is called a context switch.

In case, when threads have same priority are given equal treatment by the java schedular and, therefore they share the processor on a first-come , first-serve basic. Java permits us to set the priority of a thread using setPriority() method as :
ThreadName.setPriority(int number);

The int number is an integer value to which the thread priority is set. The thread class defines several priority constants:
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10

The int number may assume one of these constants or any value between 1 to 10. Note the default priority is NORM_PRIORITY.

When multiple threads are ready for execution , the java system chooses the highest priority thread and execute it.For a thread of lower priority to gain control ,one of the following things should happen:
1) run()
2) sleep()
3) wait()

Example of Thread Priority:

Below Example illustrate the effect of assigning higher priority to a thread.Note that although the thread A started first, the higher priority thread B has preempted it and started printing the output first.Immediately the thread C that has been assigned the highest priority takes control over the other two threads.The Thread A is the last to complete.

class A extends Thread {
public void run(){
System.out.println("** THREAD A STARTED **");
for(int i=0;i<4;i++){
System.out.println("THHREAD A : "+i);
}
System.out.println("Exit From Thread A");
}
}
class B extends Thread {
public void run(){
System.out.println("** THREAD B STARTED **");
for(int i=0;i<4;i++){
System.out.println("THHREAD B : "+i);
}
System.out.println("Exit From Thread B");
}
}
class C extends Thread {
public void run(){
System.out.println("** THREAD C STARTED **");
for(int i=0;i<4;i++){
System.out.println("THHREAD C : "+i);
}
System.out.println("Exit From Thread C");
}
}
public class ThreadPriorityExample {

public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
a.setPriority(Thread.MAX_PRIORITY);
b.setPriority(a.getPriority());
c.setPriority(Thread.MIN_PRIORITY);
System.out.println("--Thread A Strated ");
a.start();
System.out.println("--Thread B Strated ");
b.start();
System.out.println("--Thread C Strated ");
c.start();
System.out.println("--Exit from main Thread--");
}

}
Program Output:

--Thread A Strated 
--Thread B Strated 
--Thread C Strated 
** THREAD A STARTED **
THHREAD A : 0
** THREAD B STARTED **
THHREAD A : 1
THHREAD A : 2
THHREAD A : 3
Exit From Thread A
** THREAD C STARTED **
THHREAD B : 0
THHREAD B : 1
--Exit from main Thread--
THHREAD B : 2
THHREAD B : 3
Exit From Thread B
THHREAD C : 0
THHREAD C : 1
THHREAD C : 2
THHREAD C : 3
Exit From Thread C


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment