In this tutorial you will learn how to get thread priority in java thread.
Thread getPriority() :
Thread scheduler uses thread priority concept to assign priority to the thread. A higher priority threads execute first then the lower priority threads. You can set priority by using method setPriority () and get it by using method getPriority ().
public final int getPriority() : Return type is int. This method returns the priority of your thread.
Example :
class ThreadGetPriority implements Runnable{
Thread thread;
public ThreadGetPriority(){
thread = new Thread(this);
thread.start();
}
public void run() {
/* getting Priority of the thread*/
System.out.println( " Priority of "+thread.getName()+" is "+thread.getPriority());
/* setting Priority of the thread*/
thread.setPriority(6);
/* getting Priority of the thread*/
System.out.println(" New Priority of "+thread.getName()+" is "+thread.getPriority());
}
public static void main(String[] args) {
new ThreadGetPriority();
}
}
Output :
Priority of Thread-0 is 5 New Priority of Thread-0 is 6
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.