Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Thread Priority

In Java, a thread executes when it gets its CPU time, and when a thread gets CPU time, it depends on the current CPU scheduling algorithm.

When a thread is created, JVM assigns a number to the thread, which is called the priority of the thread. In a Priority-Based Scheduling algorithm, the thread with the highest priority gets CPU time first.

There are three different types of Thread Priority:

1. MIN_PRIORITY: Minimum priority of the thread will be 1.
2. MAX_PRIORITY: Maximum priority of the Thread will be 10.
3. NORM_PRIORITY: Normal priority of the Thread will be 5.

When we create a thread, JVM internally assigns a default priority, which is normal priority with a value of 5. However, using the setPriority(int) and getPriority() methods of the java.lang.Thread class, we can set or get the priority of the thread, respectively.

a. public final void setPriority(int);

Access Modifier: - public
Return Type: - void
Method Name: - setPriority
Parameter: - int
Functionality: - As we can see, the setPriority() method is an instance method that takes an int type parameter. When we invoke the setPriority() method of a thread class object and pass an int type argument, that argument is assigned to the current thread as its priority. The thread priority must be set in the range of 1 to 10. If we attempt to assign an illegal priority, we will encounter an IllegalArgumentException at runtime.

b. public final int getPriority();

Access Modifier: - public
Return Type: - int
Method Name: - getPriority
Functionality: - When we invoke getPriority() method then it return the priority of the current thread.

            
class JTC{
	public static void main(String arg[]){
		Thread t1 = new Thread();
		System.out.println("default-priority :- "+t1.getPriority());
		t1.setPriority(10);
		System.out.println("thread-priority :- "+t1.getPriority());
		t1.setPriority(1);
		System.out.println("thread-priority :- "+t1.getPriority());
		t1.setPriority(0);
	}
}
    default-priority :- 5
    thread-priority :- 10
    thread-priority :- 1
    Exception in thread "main" java.lang.IllegalArgumentException
            at java.lang.Thread.setPriority(Thread.java:1089)
            at JTC.main(test.java:9)
            

Note:- There is no use of Thread Priority when CPU scheduling algorithm is not Priority based algorithm.

Ques:-What happens when the current working CPU scheduling algorithm is a priority-based algorithm and more than one thread has the same priority

In that case, when more than one thread falls under the priority-based scheduling algorithm and has the same priority, the system may attempt to switch to another scheduling algorithm. Based on that, it can select a specific thread. We cannot predict which scheduling algorithm will be adopted by the CPU scheduler.