Share on Google+Share on Google+

Java :Thread getPriority Example

Advertisement
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

Advertisements

Posted on: November 6, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.