Javatpoint Logo
Javatpoint Logo

setPriority() Method in Java

Multithreading is a powerful concept in Java that allows you to run multiple threads concurrently within a single process. Each thread has its own execution path, allowing for improved program performance and responsiveness. However, managing these threads and controlling their execution order can be challenging. One way to manage thread execution in Java is by setting thread priorities. In this section, we will explore thread priorities in Java, including what they are, how to set them, and their impact on program execution.

Understanding Thread Priorities

In Java, each thread is assigned a priority that helps the Java Virtual Machine (JVM) determine the order in which threads are scheduled for execution. Thread priorities are represented as integers and range from 1 (the lowest priority) to 10 (the highest priority). By default, all threads in a Java program have a priority of 5.

Setting Thread Priorities

We can set the priority of a thread using the setPriority() method provided by the Java Thread class. Here's an example of how to set the priority of a thread:

SetPriorityExample.java

Output:

Thread 1: 1
Thread 1: 2
Thread 1: 3
Thread 1: 4
Thread 1: 5
Thread 2: 1
Thread 2: 2
Thread 2: 3
Thread 2: 4
Thread 2: 5

In this example, we have two threads, thread1 and thread2, each with a different priority. thread1 is set to the lowest priority using Thread.MIN_PRIORITY, while thread2 is set to the highest priority using Thread.MAX_PRIORITY.

Impact of Thread Priorities

Thread priorities are used by the JVM's thread scheduler to determine the order in which threads are executed. Threads with higher priorities are given preference over threads with lower priorities. However, it's important to note that thread priorities are only hints to the JVM, and the actual behavior may vary across different platforms and JVM implementations.

To understand the impact of thread priorities, let's consider an example where we have three threads with different priorities:

PriorityExample2.java

Output:

Thread 1: 0
Thread 2: 0
Thread 3: 0
Thread 3: 1
Thread 3: 2
Thread 3: 3
Thread 3: 4
Thread 2: 1
Thread 1: 1
Thread 2: 2
Thread 1: 2
Thread 2: 3
Thread 1: 3
Thread 2: 4
Thread 1: 4

In this example, thread1 has the lowest priority, thread2 has the default priority, and thread3 has the highest priority. When you run this program, you may observe that thread3 gets more CPU time compared to the other threads because it has the highest priority. However, the exact behavior may vary depending on your system and JVM.

It's important to use thread priorities judiciously and not rely solely on them for critical synchronization or coordination between threads. In many cases, it's better to use higher-level synchronization mechanisms such as locks, semaphores, or barriers to control thread execution.

Thread Priority and Runnable Threads

Thread priorities are associated with the threads, not with the Runnable objects they execute. It means that if you have multiple threads sharing the same Runnable object, they will all inherit the same priority set on the parent thread. Consider the following example:

PriorityExample3.java

Output:

Thread-1: 0
Thread-1: 1
Thread-1: 2
Thread-1: 3
Thread-1: 4
Thread-0: 0
Thread-0: 1
Thread-0: 2
Thread-0: 3
Thread-0: 4

In Summary, Thread priorities in Java provide a way to influence the order in which threads are scheduled for execution by the JVM's thread scheduler. However, thread priorities should be used with caution, as they are platform-dependent and may not guarantee precise control over thread execution.

In most of the cases, it's better to rely on higher-level synchronization mechanisms and design patterns to coordinate the behaviour of threads in a Java program. Thread priorities are just one tool in our tool box for managing multithreaded applications, and their use should be considered carefully based on specific requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA