Java :Thread getPriority Example


 

Java :Thread getPriority Example

In this tutorial you will learn how to get thread priority in java thread.

In this tutorial you will learn how to get thread priority in java thread.

Java :Thread getPriority Example

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

Ads