Home > Java > javaTutorial > body text

Java Thread Priority

WBOY
Release: 2024-08-30 16:02:56
Original
377 people have browsed it

Termed as the “smallest unit of processing”, Thread is a light weight subprocess assigned with some work that needs to be performed. Threads share the same memory slot assigned to them and are independent of each other, thus promotes multitasking. But when multiple threads are running on the shared memory slot, then there is bound to happen a competition on the resource. To avoid this competition so that high throughput can be achieved, a concept of prioritizing threads was introduced. It has a big significance when multiple tasks are running on the same system. The “thread scheduler does the work of assigning executing threads as per the priority”.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

JVM (JAVA virtual machine) makes the decision on prioritising thread by default or by the programmer explicitly. The priority degree ranges between 1 to 10, 10 being assigned when we want to give thread the highest priority. Context switch changes help in the transition from thread 1 to thread 2 and so on as per the priority order.

Note: There may be a chance that two or more threads are assigned with the same priority, then their execution depends upon the operating system. For example, Windows use a round-robin algorithm to handle this case.

Variables of Java Thread Priority

There are three main variables pre-saved in JAVA in the form of macros are explained below-

  • Public Static int MIN_PRIORITY: This is a static variable with an access modifier of the “public” type. This variable is assigned with a value of 1. This is to assign a thread with the lowest priority.
  • Public Static int NORM_PRIORITY: This is a static variable with an access modifier of the “public” type. This variable is assigned with a value of 5. This is to assign a thread with normal priority. It is a default priority when priority is not assigned explicitly by the developer.
  • Public Static int MAX_PRIORITY: This is a static variable with an access modifier of the “public” type. This variable is assigned with a value of 10. This is to assign a thread with the highest priority.

Some functions associated with getting and setting the priority are:

  • Public Final int getPriority (): This function is used to get the priority of any thread asked for. This function returns an integer as its return type is “int”. The integer can range between 1 to 10. The function is public and final.
  • Public Final void setPriority (int newPriority): This function is used to set the priority of any thread asked for. This function takes an integer as a parameter, and the same is mentioned in the parameter prototype in the function definition. The parameter integer can range between 1 to 10. The function is public and final.

Examples of Java Thread Priority

Following are the examples of java thread priority are:

Example #1

Below are some examples demonstrating the thread priority concept using the above already defined variables and ready-made functions available in JAVA.

Code:

public class test extends Thread{
public void run (){
System.out.println ( "The name of thread running curremtly is :"+Thread.currentThread ().getName ());
System.out.println ( "The priority od thread running currently is:"+Thread.currentThread ().getPriority ());
}
public static void main (String args[]){
test t1=new test ();
test t2=new test ();
test t3=new test ();
t1.setPriority (Thread.MIN_PRIORITY);
t2.setPriority (Thread.MAX_PRIORITY);
t3.setPriority (Thread.NORM_PRIORITY);
t1.start ();
t2.start ();
t3.start ();
}
}
Copy after login

Output:

Java Thread Priority

Example #2

Below is an example of user-defined priority definition and printing.

Code:

public class test2 extends Thread
{
public void run ()
{
System.out.println ( " The control is under run function now...");
}
public static void main (String args[])
{
// Here we are creating threads using the constructors.
test2 t1=new test2 ();
test2 t2=new test2 ();
// setpriority () function is used below along with the parameter to set the prioirity.
t1.setPriority (2);
t2.setPriority (9);
// Here we are coding on how to display output strings.
System.out.println ( " The priority assigned to thread t1 is: " + t1.getPriority ());
System.out.println ( "The priority assigned to thread t2 is: " + t2.getPriority ());
// the run () function is defined above will be called via start () function and print the strinf which is there in it.
t1.start ();
}
}
Copy after login

Output:

Java Thread Priority

Note: The priority should strictly fall under the range of 1 to 10. In case the priority lies out of this range, then the compiler throws the below error. I got this error when 13 was given a priority in the place of 9 while setting a priority of thread t2 using the setPriority () function.

Exception:

Exception in thread “main” java.lang.IllegalArgumentException

at java.base/java.lang.Thread.setPriority (Thread.java:1141)

at test2.main (test2.java:14)

Advantages of Java Thread Priority

There are many advantages associated with multithreading and the assigning the priority to thread listed below:

  • It allows multiple operations to be performed simultaneously in the system, along with the thread’s priority. For example, the user is surfing on the internet but suddenly interrupts the system as new software has been installed. In this case, priority is given to restarting the system over internet surfing.
  • The JAVA thread inherits its priority from parent tread if the programmer does not explicitly define thread priority. There is the retention of priority bypassing the priority in the downstream threads and maintaining symmetricity. It makes it easy to debug the program by programmers.
  • It makes a code simpler thus easy to maintain.
  • It makes the work of context switch much easier by assigning priorities.

Conclusion

This is one of the widely used and efficient ways to operate multiple tasks in the same system. Since threads share the memory, this memory-efficient way as well. We can get multiple threads running in the system, but that may confuse the processor upon which one to choose first. This issue was solved with the help of assigning priorities to the thread. The thread keeps on running until it finishes or is interrupted by a thread of higher priority. This functionality works closely with the operating system. Preparing word documents along with internet surfing with music was not so efficient until the advent of the magical concept of multi-threading.

The above is the detailed content of Java Thread Priority. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template