Thread Priority Concept Simplified in Java



In my earlier post here I have shown details about a thread and how to create a thread. In this post we will see an important property of a thread called Priority.In java each thread is assigned a priority which effects the execution of the thread.By default threads will be created with normal priority and threads with having same priority are given equal time slot to execute. Mostly it is first come first serve basis.
Whenever multiple threads are ready for execution ,the java will choose the highest priority thread and will execute.For a lower priority thread to gain control ,one of the following condition should happen to a high priority thread:

  1. It stops running at the end of the run().
  2. It is made to sleep using sleep()
  3. It is made to wait using wait().
If another thread of a higher priority comes along the currently running thread will be preempted by the incoming thread along,forcing the current thread to move to runnable state.Higher priority threads will always preempts any lower priority thread.
How to set priority to a thread:

ThreadName.setPriority(int priorityNumber);
 
int priorityNumber is a constant ranging from 0 to 10. Most user level processes should be NORM_PRIORITY+/- 1
An example:

class A extends Thread{
    public void run(){
        System.out.println("Running Thread A");
        for(int i=0;i<10;i++){
          System.out.println("Running from Thread A "+i);   
        }
         System.out.println("Ending Thread A");
    }
}
class B extends Thread{
    public void run(){
        System.out.println("Running Thread B");
        for(int i=0;i<10;i++){
          System.out.println("Running from Thread B "+i);   
        }
         System.out.println("Ending Thread B");
    }
}
class C extends Thread{
    public void run(){
        System.out.println("Running Thread C");
        for(int i=0;i<10;i++){
          System.out.println("Running from Thread C "+i);   
        }
         System.out.println("Ending Thread C");
    }
}
public class ThreadPriorityTest{
    public  static void main(String args[])
    {
        A threadA=new A();
        B threadB=new B();
        C threadC=new C();
        threadC.setPriority(Thread.MAX_PRIORITY);
        threadB.setPriority(threadA.getPriority()+2);
        threadA.setPriority(Thread.MIN_PRIORITY);
        System.out.println("Start thraed A");
        threadA.start();
        System.out.println("Start thraed B");
        threadB.start();
        System.out.println("Start thraed C");
        threadC.start();
        System.out.println("Out of main thread");
    }
}
 
output of the code:
$javac ThreadPriorityTest.java
$java -Xmx128M -Xms16M ThreadPriorityTest
Start thraed A
Start thraed B
Running Thread A
Start thraed C
Running from Thread A 0
Running from Thread A 1
Running from Thread A 2
Running from Thread A 3
Running Thread B
Out of main thread
Running from Thread A 4
Running from Thread B 0
Running from Thread B 1
Running Thread C
Running from Thread A 5
Running from Thread C 0
Running from Thread B 2
Running from Thread B 3
Running from Thread B 4
Running from Thread B 5
Running from Thread B 6
Running from Thread B 7
Running from Thread B 8
Running from Thread B 9
Ending Thread B
Running from Thread C 1
Running from Thread C 2
Running from Thread C 3
Running from Thread C 4
Running from Thread C 5
Running from Thread C 6
Running from Thread C 7
Running from Thread C 8
Running from Thread C 9
Ending Thread C
Running from Thread A 6
Running from Thread A 7
Running from Thread A 8
Running from Thread A 9
Ending Thread A
Thread Priority Concept Simplified in Java Thread Priority Concept Simplified in Java Reviewed by Animesh Chatterjee on December 05, 2018 Rating: 5

No comments:

Powered by Blogger.