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.There are three main variables pre-saved in JAVA in the form of macros are explained below-
Some functions associated with getting and setting the priority are:
Following are the examples of java thread priority are:
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 (); } }
Output:
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 (); } }
Output:
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)
There are many advantages associated with multithreading and the assigning the priority to thread listed below:
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!