Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconThread Priority in Java: Explained with Examples

Thread Priority in Java: Explained with Examples

Last updated:
3rd Feb, 2023
Views
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
Thread Priority in Java: Explained with Examples

Fundamentals of Thread Priority in Java:

Though concurrent execution of threads is conceptually true, it is impractical. Most of the computer configurations encompass a single Central Processing Unit and hence only one thread can be executed at a time. So, the practical implementation of concurrency of threads is an illusion. The ordered execution of several threads on a single CPU is referred to as scheduling. Java supports a deterministic and simple scheduling algorithm called the fixed priority scheduling. The algorithm enables the scheduling of threads on the basis of their priority with respect to the other threads that can be executed. 

At the time of the creation of threads in Java, the thread that creates a new thread induces the priority for the newly created thread. The setPriority method can also be used to set or modify the precedence of a thread after it is created. The thread priority in Java is an integer value that ranges between MIN_PRIORITY and MAX_PRIORITY. These constants are defined in the Thread Class. The higher the value of the integer, the higher the thread priority.

When several threads are ready for execution at any given time, a thread with the highest priority is chosen by the Java runtime for execution. A thread with the immediate lower priority will start its execution only after the higher priority thread stops or yields or becomes inexecutable for some reason. The scheduler uses a round-robin fashion when two threads with the same priority are to be run. The thread chosen in this fashion will be executed until one among the below-mentioned conditions is satisfied:

  • A thread with higher priority is executable.
  • The thread yields or exits the run method.
  • The allocated time of the thread has expired on systems that facilitates time slicing.

Once any one of the above-mentioned conditions is satisfied, a thread with the next higher priority is given the chance of execution. This process continues until the Java interpreter exists.

Ads of upGrad blog

Check out our free courses to get an edge over the competition.

The thread scheduling algorithm of the Java runtime system is also pre-emptive. When a thread with the highest priority among all the threads that can run becomes executable, the runtime system allows the execution of this new thread with the highest priority. In such cases, all other threads are said to be preempted by the thread with the highest priority.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Setting and Getting thread priority in Java:

The default value of thread priority in Java is 5. However, the thread priority can be modified to any value ranging between 1 to 10. Java’s Thread class has three static variables of priority to the threads in a multithreading system. They are:

  • public static int MIN_PRIORITY
  • public static int NORM_PRIORITY
  • public static int MAX_PRIORITY

public static int MIN_PRIORITY

It is the minimum value of the precedence of a thread in Java. The minimum thread priority in Java corresponds to the value ‘0’.

Check out upGrad’s Advanced Certification in DevOps 

public static int NORM_PRIORITY

This refers to the default value assigned by the Java Virtual Machine to the thread priority. The normal thread priority in Java corresponds to ‘5’.

public static int MAX_PRIORITY

This variable corresponds to the maximum value to which the thread can be prioritized. The maximum value of thread priority in Java is 10.

Java facilitates the developer to check and assign or modify thread priorities. There are two methods in Java’s Thread class to support this task. They are:

  • public final int getPriority():

This method is invoked to return the priority of a Java thread. It is a final and public method. It returns an int type value.

  • public final void setPriority(int newPriority):

With the invocation of this method, the priority of a Java thread can be altered. It is also a final and public method. The method returns nothing and hence it is said to return a void type. 

Ads of upGrad blog

newPriority: This function accepts an int type argument. By calling this method, a developer can assign a new thread priority value by specifying an appropriate integer value in the argument. However, the integer value should be a number between 1 and 10. Suppose the value exceeds 10, an IllegalArgumentException is returned.

Check out upGrad’s Advanced Certification in Cyber Security

Limitations of Thread Priority in Java:

The execution of the highest priority thread is not assured by the Java thread scheduler. Sometimes, a thread with lower thread priority may also be executed by the scheduler to dodge starvation. Because of this, the thread priority in Java can be used only to impact the scheduling policy for the purpose of efficiency. Thread priority cannot be relied upon for determining the correctness of the algorithm.

Do you want to learn the core concepts in Java? The upGrad Executive Post Graduate Programme in Software Development – Specialisation in Full Stack Development course is an online 7 weeks program for beginners who want to explore software development careers. Attend live and interactive classes, practice sessions, and over 35 hours of content delivered by industry leaders to earn a completion certificate from upGrad. 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Top 14 Technical Courses to Get a Job in IT Field in India [2024]
90952
In this Article, you will learn about top 14 technical courses to get a job in IT. Software Development Data Science Machine Learning Blockchain Mana
Read More

by upGrad

15 Jul 2024

25 Best Django Project Ideas & Topics For Beginners [2024]
143863
What is a Django Project? Django projects with source code are a collection of configurations and files that help with the development of website app
Read More

by Kechit Goyal

11 Jul 2024

Must Read 50 OOPs Interview Questions & Answers For Freshers & Experienced [2024]
124781
Attending a programming interview and wondering what are all the OOP interview questions and discussions you will go through? Before attending an inte
Read More

by Rohan Vats

04 Jul 2024

Understanding Exception Hierarchy in Java Explained
16879
The term ‘Exception’ is short for “exceptional event.” In Java, an Exception is essentially an event that occurs during the ex
Read More

by Pavan Vadapalli

04 Jul 2024

33 Best Computer Science Project Ideas & Topics For Beginners [Latest 2024]
198249
Summary: In this article, you will learn 33 Interesting Computer Science Project Ideas & Topics For Beginners (2024). Best Computer Science Proje
Read More

by Pavan Vadapalli

03 Jul 2024

Loose Coupling vs Tight Coupling in Java: Difference Between Loose Coupling & Tight Coupling
65177
In this article, I aim to provide a profound understanding of coupling in Java, shedding light on its various types through real-world examples, inclu
Read More

by Rohan Vats

02 Jul 2024

Top 58 Coding Interview Questions & Answers 2024 [For Freshers & Experienced]
44555
In coding interviews, a solid understanding of fundamental data structures like arrays, binary trees, hash tables, and linked lists is crucial. Combin
Read More

by Sriram

26 Jun 2024

Top 10 Features & Characteristics of Cloud Computing in 2024
16289
Cloud computing has become very popular these days. Businesses are expanding worldwide as they heavily rely on data. Cloud computing is the only solut
Read More

by Pavan Vadapalli

24 Jun 2024

Top 10 Interesting Engineering Projects Ideas & Topics in 2024
43094
Greetings, fellow engineers! As someone deeply immersed in the world of innovation and problem-solving, I’m excited to share some captivating en
Read More

by Rohit Sharma

13 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon