In this article, we will see about Thread Priority in Java with example. In multithreading, each thread has some priority. Even we write a simple hello world program, we have default thread (main thread) has default priority 5. If we create user defined threads JVM will assign default priority i.e 5 for these threads too.
Let’s see an example.
package threadpriority;
class MyThread1 extends Thread {
public void run() {
System.out.println("Name of current thread is " + Thread.currentThread().getName());
System.out.println("Priority of current thread is " + Thread.currentThread().getPriority());
}
}
public class ThreadPriorityExample {
public static void main(String[] args) {
MyThread1 thread1 = new MyThread1();
thread1.start();
System.out.println("Name of current thread is " + Thread.currentThread().getName());
System.out.println("Priority of current thread is " + Thread.currentThread().getPriority());
}
}
Output is –
Name of current thread is main
Priority of current thread is 5
Name of current thread is Thread-0
Priority of current thread is 5
Range of thread priority – The range of thread priority is between 1 to 10. There are three constant has been defined in thread class MIN_PRIORITY, NORM_PRIORITY and MAX_PRIORITY as below.
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
Example –
package threadpriority;
public class ThreadPriorityExample {
public static void main(String[] args) {
System.out.println("Minimum priority of thread is "+Thread.MIN_PRIORITY);
System.out.println("Normal priority of thread is "+Thread.NORM_PRIORITY);
System.out.println("Maximum priority of thread is "+Thread.MAX_PRIORITY);
}
}
Output is –
Minimum priority of thread is 1
Normal priority of thread is 5
Maximum priority of thread is 10
Setting a thread priority – We can set thread priority using setPriority() method.
package threadpriority;
public class ThreadPriorityExample {
public static void main(String[] args) {
System.out.println("Name of thread is :- "+Thread.currentThread().getName());
System.out.println("Default priority of thread is :- "+Thread.currentThread().getPriority());
System.out.println("----After setting priority----");
Thread.currentThread().setPriority(8);
System.out.println("Name of thread is :- "+Thread.currentThread().getName());
System.out.println("Default priority of thread is :- "+Thread.currentThread().getPriority());
}
}
Output is –
Name of thread is :- main
Default priority of thread is :- 5
—-After setting priority—-
Name of thread is :- main
Default priority of thread is :- 8
We can also define priority for user defined threads.
package threadpriority;
class MyThread1 extends Thread {
public void run() {
System.out.println(Thread.currentThread().getPriority());
}
}
public class ThreadPriorityExample {
public static void main(String[] args) {
MyThread1 thread1 = new MyThread1();
MyThread1 thread2 = new MyThread1();
MyThread1 thread3 = new MyThread1();
thread1.setPriority(6);
thread2.setPriority(7);
thread3.setPriority(8);
thread1.start();
thread2.start();
thread3.start();
}
}
Output is –
6
7
8
The sequence of output may vary. Since we are doing thread1.start(), thread2.start() and thread3.start() , run() method will call three times.
That’s all about Thread Priority in Java with example.
You may like.
- Thread sleep() method in Java.
- Thread join() method in Java.
- Thread yield() method in Java with Example.
- How run() method get called using start() method.
- Daemon Thread In Java With Example.
- Synchronization In Java With Example.
- Thread class constructors and methods in Java.
- How to create a thread in Java.
- wait() notify() and notifyAll() method in Java.
Thread class docs.
