The getPriority()
method of Thread
class.this
thread. When we create a thread/process, the Thread
scheduler in Java assigns some priority to it. The thread priority ranges from 1 to 10, while the default priority value of a thread is 5.
Note: The
Thread
scheduler assigns priority implicitly. The programmer can also set this priority explicitly by invoking the setPriority() method.
The syntax for the getPriority()
method is given below:
final int getPriority()
This method does not take any parameter values.
This method returns an integer value. This value ranges from 1 to 10 as a priority number in the process priority queue.
Here, we have an example to demonstrate the working the getPriority()
method:
// Program to use getPriority()// Main class which extends Thread classpublic class Main extends Thread {public void run() {System.out.println("Running thread name: "+ Thread.currentThread().getName());}public static void main(String args[]) {// create two threadsMain thread1 = new Main();Main thread2 = new Main();// chnage thread namesthread1.setName("thread1");thread2.setName("thread2");// print the default priority value of threadSystem.out.println("thread1 priority : " + thread1.getPriority());System.out.println("thread2 priority : " + thread2.getPriority());// this will call the run() methodthread1.start();thread2.start();}}
Main.class
.getPriority()
method for each thread. Keep in mind that the priority of the threads depends upon start()
method to start the execution of the threads.