0% found this document useful (0 votes)
551 views

Priority of A Thread in Java - Javatpoint

Each Java thread has a priority level between 1-10 that determines its likelihood of being scheduled by the thread scheduler, with higher numbers being more likely to run. The default priority is 5. The Thread class defines constants for the minimum, normal, and maximum priority levels. An example program demonstrates setting one thread to the minimum priority and another to the maximum priority, then starting them to show how priority affects scheduling order.

Uploaded by

Jay The Perfect
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
551 views

Priority of A Thread in Java - Javatpoint

Each Java thread has a priority level between 1-10 that determines its likelihood of being scheduled by the thread scheduler, with higher numbers being more likely to run. The default priority is 5. The Thread class defines constants for the minimum, normal, and maximum priority levels. An example program demonstrates setting one thread to the minimum priority and another to the maximum priority, then starting them to show how priority affects scheduling order.

Uploaded by

Jay The Perfect
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

11/8/2019 Priority of a Thread in Java - javatpoint

Priority of a Thread (Thread Priority):


Each thread have a priority. Priorities are represented by a number between 1 and 10. 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 10.

Example of priority of a Thread:

class TestMultiPriority1 extends Thread{


public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+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();

}
}

Test it Now

Output:running thread name is:Thread-0


running thread priority is:10
running thread name is:Thread-1
running thread priority is:1

https://www.javatpoint.com/priority-of-a-thread 1/1

You might also like