🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, each thread is assigned a priority that helps the thread scheduler decide the order in which threads are executed. Thread priority is an integer value that ranges from Thread.MIN_PRIORITY (1) to Thread.MAX_PRIORITY (10). The default priority is Thread.NORM_PRIORITY (5). The thread scheduler uses these priorities to decide when each thread should run. However, thread priority does not guarantee the order of execution; it is merely a suggestion to the thread scheduler.
Table of Contents
- Setting Thread Priority
- Getting Thread Priority
- Example: Setting and Getting Thread Priority
- Conclusion
1. Setting Thread Priority
You can set a thread's priority using the setPriority(int newPriority) method of the Thread class. The new priority must be within the range of Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.
Syntax:
public final void setPriority(int newPriority)
2. Getting Thread Priority
You can get a thread's priority using the getPriority() method of the Thread class. This method returns the priority of the thread as an integer.
Syntax:
public final int getPriority()
3. Example: Setting and Getting Thread Priority
Let's create an example to demonstrate how to set and get the priority of a thread.
Example:
class MyThread extends Thread {
public MyThread(String name) {
super(name); // Call the parent constructor to set the thread name
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + " with priority " +
Thread.currentThread().getPriority() + " is running.");
}
}
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread-1");
MyThread thread2 = new MyThread("Thread-2");
MyThread thread3 = new MyThread("Thread-3");
// Set the priorities
thread1.setPriority(Thread.MIN_PRIORITY); // Priority 1
thread2.setPriority(Thread.NORM_PRIORITY); // Priority 5
thread3.setPriority(Thread.MAX_PRIORITY); // Priority 10
// Start the threads
thread1.start();
thread2.start();
thread3.start();
// Display the priorities
System.out.println(thread1.getName() + " priority: " + thread1.getPriority());
System.out.println(thread2.getName() + " priority: " + thread2.getPriority());
System.out.println(thread3.getName() + " priority: " + thread3.getPriority());
}
}
Output:
Thread-1 priority: 1
Thread-2 priority: 5
Thread-3 priority: 10
Thread-3 with priority 10 is running.
Thread-3 with priority 10 is running.
Thread-3 with priority 10 is running.
Thread-2 with priority 5 is running.
Thread-2 with priority 5 is running.
Thread-2 with priority 5 is running.
Thread-1 with priority 1 is running.
Thread-1 with priority 1 is running.
Thread-1 with priority 1 is running.
Explanation:
- The
MyThreadclass extends theThreadclass and calls the parent constructor to set the thread name. - The
runmethod prints the name and priority of the current thread. - In the
mainmethod, threeMyThreadobjects are created with custom names. - The priorities of the threads are set using the
setPrioritymethod. - The threads are started using the
startmethod. - The priorities of the threads are printed using the
getPrioritymethod.
4. Conclusion
Thread priority in Java is a useful feature that helps the thread scheduler decide the order in which threads should run. By setting and getting thread priorities, you can influence the execution of threads in your programs. However, keep in mind that thread priority is only a suggestion to the thread scheduler and does not guarantee the order of execution. This guide provided an example of how to set and get thread priorities in Java.
Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
http://www.javaguides.net/p/java-multithreading-utorial.html
ReplyDelete