In: Computer Science
3. Write a Java program to demonstrate the concept of priority thread. It should show that each thread have a priority. Priorities are represented by a number between 1 and 20. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
3 constants defined in Thread class:
1. public static int MIN_PRIORITY
2. public static int NORM_PRIORITY
3. public static int MAX_PRIORITY
Default priority of a thread is 5 (NORM_PRIORITY). The value of
MIN_PRIORITY is 1 and
the value of MAX_PRIORITY is 20.
Here is an example of priority of a Thread:
1. class TestMultiPriority1 extends Thread{
2. public void run(){
3. System.out.println("running thread
name is:"+Thread.currentThread().getName());
4. System.out.println("running thread
priority is:"+Thread.currentThread().getPriority());
5. }
6. public static void main(String args[]){
7. TestMultiPriority1 m1=new
TestMultiPriority1();
8. TestMultiPriority1 m2=new
TestMultiPriority1();
9. m1.setPriority(Thread.MIN_PRIORITY);
10. m2.setPriority(Thread.MAX_PRIORITY);
11. m1.start();
12. m2.start();
13.
14. }
15. }
public class TestMultiPriority1 extends Thread {
public void run() {
// printing the name and Priority
of current running thread
System.out.println(
"Name : " + Thread.currentThread().getName() + "
: Priority : " + Thread.currentThread().getPriority());
}
public static void main(String args[]) {
TestMultiPriority1 m1 = new
TestMultiPriority1();
TestMultiPriority1 m2 = new
TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me