Java Thread Priority in Multithreading
As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string. Whenever we make a string in Java, it has some need doled out. The market can either be given by JVM while making the string, or it may be provided by the developer unequivocally.
Needs in strings is an idea where each string is vital, in layman's language, one can say each item has a need here, which is addressed by numbers going from 1 to 10.
The default need is set to 5, as excepted.
The least need is set to 1.
The most extreme need is set to 10.
Here three constants are characterised in it specifically as follows:
public static int NORM_PRIORITY
public static int MIN_PRIORITY
public static int MAX_PRIORITY
Allow us to examine it with a guide to get how inside the work is getting executed. Here we will utilise the information accumulated above as follows:
We will utilise the currentThread() technique to get the name of the ongoing string. The client can likewise utilise the setName() technique if they need to settle on the names of the string according to a decision for understanding.
getName() strategy will be utilised to get the name of the string.
The acknowledged worth of the need for a string is in the scope of 1 to 10.
Let us examine how to get and define the boundary of a string in java.
public last int getPriority(): java.lang.Thread.getPriority() strategy returns need of given string.
public last void setPriority(int newPriority): java.lang.Thread.setPriority() strategy changes the need of string to the worth newPriority. This strategy tosses IllegalArgumentException assuming that the worth of boundary newPriority goes past the minimum(1) and maximum(10) limit.
Example:
//simple Java Program to Illustrate Priorities in Multithreading
// via the help of getPriority() and setPriority() method
// here, we are importing the required classes in our program
import java.io.*; //here, we are importing the java.io package into our program
import java.util.*; //here, we are importing the java.util package into our program
import java.lang.*; //here, we are importing the java.lang package into our program
// here, we are declaring the Main class
class TD extends Thread {
//here, we are declaring the Method 1
//here, we are using the run() method for the thread that is called
// as soon as the start() method is invoked for the thread in the main() class or //method
public void run()
{
//here, we are printing the statement in the run() method
System.out.println("Inside run method");
}
//here, we are declaring the Main driver method
public static void main(String[] args)
{
// here, we are creating the random threads in our program
// with the help of the above class
TD t1 = new TD();
TD t2 = new TD();
TD t3 = new TD();
//here, we are declaring the Thread 1
// here, we are displaying the priority of the above thread
// here, we are using getPriority() method
System.out.println("The t1 thread priority is: " + t1.getPriority());
// here, we are declaring the Thread 1
// here, we are displaying the priority of the above thread
System.out.println("The t2 thread priority is: "+ t2.getPriority());
// here, we are declaring the Thread 3
System.out.println("The t3 thread priority is: " + t3.getPriority());
// here, we are setting the priorities of the above threads by
// passing integer arguments
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
// here, the t3.setPriority(21); will throw
// the IllegalArgumentException
//here, we are printing the priority of 2
System.out.println("The t1 thread priority is: " + t1.getPriority());
// here, we are printing the priority of 5
System.out.println("The t2 thread priority is: "+ t2.getPriority());
// here, we are printing the priority of 8
System.out.println("The t3 thread priority is: " + t3.getPriority());
// here, we are declaring the Main thread
// here, we are Displays the name of
// the currently executing Thread
System.out.println( "Currently we are Executing the Thread : "+ Thread.currentThread().getName());
System.out.println("The Main thread priority is: "+ Thread.currentThread().getPriority());
// here, we are declaring that the Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println( "The Main thread priority is: "+ Thread.currentThread().getPriority());
}
}
Output:
The t1 thread priority is: 5
The t2 thread priority is: 5
The t3 thread priority is: 5
The t1 thread priority is: 2
The t2 thread priority is: 5
The t3 thread priority is: 8
Currently, we are Executing the Thread: main
The main thread priority is: 5
The Main thread priority is: 10
Explanation:
- Thread with the most noteworthy need will get an execution opportunity preceding different strings. Assume there are 3 strings, t1, t2, and t3, with needs 4, 6, and 1. In this way, string t2 will execute first in view of most extreme need 6 after that, t1 will execute and afterwards, t3.
- The default need for the fundamental string is dependably 5, it tends to be changed later. The default needs for any remaining strings rely upon the need of the parent thread.
Example 1:
// simple Java program to demonstrate that a Child thread
// is getting the Same Priority as the Parent thread
// here, we are importing all classes from the java.lang package into our program
import java.lang.*;
// here, we are declaring the Main class
// here, we are creating the TD that
// Extends the Thread class
class G1 extends Thread {
// here, we are declaring the Method 1
// here, we are using the run() method for the thread that is
// invoked when the threads are started in the main() method
public void run()
{
// here, we are printing the statement
System.out.println("Inside run method");
}
// here, we are declaring the Method 2
// here, we are declaring the Main driver method
public static void main(String[] args)
{
// here, the main thread priority is set to 6 now
Thread.currentThread().setPriority(6);
// here, the current thread is accessed
// using the currentThread() method
// here, we are printing and displaying the main thread priority
// using the getPriority() method of Thread class
System.out.println(
"The main thread or the parent thread priority is: "
+ Thread.currentThread().getPriority());
// here, we are creating a thread by creating an object inside
// the main() method
G1 t1 = new G1(); //here, we are creating the object to the main() method G1
// here, the t1 thread is the child thread of the main thread
// so the t1 thread will also have the same priority 6
// here, we are printing and displaying the priority of the current thread
System.out.println("The t1 thread or the child thread priority is: " + t1.getPriority());
}
}
Output:
The main thread or the parent thread priority is: 6
The t1 thread or the child thread priority is: 6
Explanation:
- On the off chance that two strings have a similar need, we can't expect which string will execute first. It relies upon the string scheduler's algorithm(Round-Robin, The early bird gets the worm, and so on)
- On the off chance that we are involving string need for string booking, we ought to constantly remember that the hidden stage ought to offer help for planning in view of string need.
Example of IllegalArgumentException:
//simple java program to understand the IllegalArgumenteaception concept
// importing the java.lang package
import java.lang.*;
public class IllegalArgumentException extends Thread
{
// here, we are declaring the main method
public static void main(String argvs[])
{
// Now, the priority of the main thread is set to 17, which is greater than 10
Thread.currentThread().setPriority(17);
// here, the current thread is retrieved
// using the currentThread() method
// here, we are displaying the main thread priority
// using the getPriority() method of the Thread class
System.out.println("The Priority of the main thread is : " + Thread.currentThread().getPriority());
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException
at java.base/java.lang.Thread.setPriority(Thread.java:1141)
at IllegalArgumentException.main(IllegalArgumentException.java:12)