Javatpoint Logo
Javatpoint Logo

Java setPriority() Method

In Java, when working with multi-threaded applications, it is crucial to manage thread priorities effectively. Prioritizing threads can help us to control how the operating system schedules threads for execution. Java provides a method called setPriority() to set the priority of a thread, allowing us to influence the order in which threads are executed. In this section, we will explore the setPriority() method in Java in detail, including its usage, explanation, and complete code examples.

Thread Priority

In Java, threads are scheduled for execution by the operating system's thread scheduler. Each thread has a priority associated with it, which is an integer value ranging from 1 to 10. Threads with higher priority values are given preference by the scheduler and tend to be executed before lower-priority threads. However, thread priorities do not guarantee strict execution order, as they depend on the underlying operating system.

The Thread class in Java provides the setPriority() and getPriority() methods to manipulate and retrieve the priority of a thread, respectively.

Thread Priority Constants

Java's Thread class defines three constant properties related to thread priorities:

  1. public static int MIN_PRIORITY: The constant signifies the minimum priority a thread can have, with a value of 1.
  2. public static int NORM_PRIORITY: It represents the default or normal priority, typically set to 5.
  3. public static int MAX_PRIORITY: The constant stands for the maximum priority a thread can possess, with a value of 10.

Java setPriority() Method

The setPriority() method allows us to set the priority of a thread. Its syntax is as follows:

Here, priority is the new priority we want to assign to the thread. It must be an integer within the range of MIN_PRIORITY (1) to MAX_PRIORITY (10).

Exception Handling

The setPriority() method can throw two exceptions:

  1. IllegalArgumentException: An exception is thrown if we attempt to set a priority outside the range of MIN_PRIORITY to MAX_PRIORITY.
  2. SecurityException: It is thrown if the current thread does not have permission to modify the priority of the target thread.

newPriority:

The new priority value for the thread, which should be an integer between 1 and 10, inclusive.

Example 1: Setting Maximum Priority

MaxPriorityThread.java

Output:

Running with maximum priority ...
Thread: Thread-0 - Count: 0
Thread: Thread-0 - Count: 1
Thread: Thread-0 - Count: 2
Thread: Thread-0 - Count: 3
Thread: Thread-0 - Count: 4

Explanation

In this example, we create a thread thread1 and set its priority to the maximum value using setPriority(Thread.MAX_PRIORITY).

Example 2: Setting Minimum Priority

MinPriorityThread.java

Output:

Running with normal priority...
Thread: Thread-0 - Count: 0
Thread: Thread-0 - Count: 1
Thread: Thread-0 - Count: 2
Thread: Thread-0 - Count: 3
Thread: Thread-0 - Count: 4

Explanation

Here, we set the priority of thread1 to the minimum value using setPriority(Thread.MIN_PRIORITY).

Example 3: Setting Normal Priority

NormPriorityThread.java

Output:

Running with normal priority...
Thread: Thread-0 - Count: 0
Thread: Thread-0 - Count: 1
Thread: Thread-0 - Count: 2
Thread: Thread-0 - Count: 3
Thread: Thread-0 - Count: 4

Explanation

In this case, we set the priority to the default value using setPriority(Thread.NORM_PRIORITY).

Example 4: User-Defined Priority

CustomPriorityThread.java

Output:

Running with custom priority...
Thread: Thread-0 - Count: 0
Thread: Thread-0 - Count: 1
Thread: Thread-0 - Count: 2
Thread: Thread-0 - Count: 3
Thread: Thread-0 - Count: 4
Running with custom priority...
Thread: Thread-1 - Count: 0
Thread: Thread-1 - Count: 1
Thread: Thread-1 - Count: 2
Thread: Thread-1 - Count: 3
Thread: Thread-1 - Count: 4

Explanation

In this example, we create two custom priority threads and set their priorities to 4 and 7, respectively, using setPriority().

Example 5: Handling IllegalArgumentException

InvalidPriorityThread.java

Output:

IllegalArgumentException: Priority out of range: 12
Priority of thread t2 is: 7
Running...

Explanation

In this example, we intentionally attempt to set an invalid priority (greater than 10) for thread1, which results in an IllegalArgumentException. The priority for thread2 is set to 7 and is printed as well.

Example of setPriority() Method

Let's walk through an example to see how the setPriority() method works in practice. In this example, we will create two threads with different priorities and observe their execution order:

ThreadPriorityExample.java

Output:

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

Explanation

We create two threads (thread1 and thread2) and assign them different priorities using setPriority() method. thread1 is assigned the minimum priority (1), while thread2 is assigned the maximum priority (10). Both threads execute a simple run method that counts from 0 to 4, printing their names and the current count. We start both threads.

Best Practices and Considerations

When using the setPriority() method, consider the following best practices and considerations:

  1. Avoid excessive priority tuning: Changing thread priorities should be done sparingly and with a clear understanding of the application's requirements. Excessive use of thread priorities can lead to unpredictable behavior and reduced code maintainability.
  2. Prefer high-level concurrency constructs: Whenever possible, use high-level concurrency constructs like the java.util.concurrent package, which abstracts away many low-level details, including thread priorities.
  3. Thread priority is platform-dependent: Thread priorities may not behave consistently across different operating systems and Java implementations. Rely on priorities only when necessary.
  4. Avoid depending on exact execution order: Thread priority is a hint to the operating system's scheduler, and it doesn't guarantee a precise execution order. Design your code to be thread-safe and robust regardless of the order in which threads run.

Conclusion

In Java, the setPriority() method is a tool for influencing the scheduling of threads in a multi-threaded application. While it can be useful in specific scenarios, it's essential to use it judiciously and understand that thread priority is platform-dependent and doesn't provide strict execution order guarantees. When designing multi-threaded applications, consider using higher-level concurrency constructs and prioritize writing thread-safe code over manipulating thread priorities.







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