Java setPriority() MethodIn 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 PriorityIn 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 ConstantsJava's Thread class defines three constant properties related to thread priorities:
Java setPriority() MethodThe 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 HandlingThe setPriority() method can throw two exceptions:
newPriority:The new priority value for the thread, which should be an integer between 1 and 10, inclusive. Example 1: Setting Maximum PriorityMaxPriorityThread.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 PriorityMinPriorityThread.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 PriorityNormPriorityThread.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 PriorityCustomPriorityThread.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 IllegalArgumentExceptionInvalidPriorityThread.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() MethodLet'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 ConsiderationsWhen using the setPriority() method, consider the following best practices and considerations:
ConclusionIn 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. Next TopicMutator Methods in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India