- Thread scheduler will use thread priority while allocating processor.
- Thread that has high priority will get the chance first to execute.
- Thread defines the following method to get and set priority of a thread.
- public final int getPriority()
- public final int setPriority(int p)
- Here p range is 1 to 10 otherwise RE:IllegalArguementException
Program example 1:
class MyThread extends Thread
{
}
class Priority
{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getPriority());
//System.out.println(Thread.CurrentThread().setPriority(15));
//RE:IllegalStateException
System.out.println(Thread.currentThread());
Thread t=new Thread();
t.start();
System.out.println(t.getPriority());
}
}
output:
Program example 2:
class MyThread extends Thread
{
}
class Priority
{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getPriority());
//System.out.println(Thread.CurrentThread().setPriority(15));
//RE:IllegalStateException
System.out.println(Thread.currentThread());
Thread t=new Thread();
t.start();
t.setPriority(6);
System.out.println(t.getPriority());
}
}
output: