Java Thread setPriority() Method
Example
Change the priority of a thread to the highest and the lowest value:
public class Main {
public static void main(String[] args) {
Thread thread = new Thread();
thread.setPriority(Thread.MAX_PRIORITY);
System.out.println(thread.getPriority());
thread.setPriority(Thread.MIN_PRIORITY);
System.out.println(thread.getPriority());
}
}
Definition and Usage
The setPriority() method changes the priority of a thread. The priority is a number from 1 (lowest) to 10 (highest). The normal priority is 5.
A thread with a higher priority may get more time to run, but this depends on the operating system. Do not use priorities to decide the order in which threads run.
Syntax
public final void setPriority(int newPriority)
Parameter Values
| Parameter | Description |
|---|---|
| newPriority | Required. A number from 1 to 10. You can use the constants Thread.MIN_PRIORITY (1), Thread.NORM_PRIORITY (5) and Thread.MAX_PRIORITY (10). |
Technical Details
| Throws: | IllegalArgumentException - if the priority is less than 1 or greater than 10. |
|---|
Related Pages
❮ Thread Methods