Priority of thread

Java uses priority based scheduling to schedule execution of threads. Thread having highest priority will run first. Thread with low priority will execute after execution of all high priority thread.

if two thread having same priority level then FIFO is used to schedule their execution order. By default every thread has same priority which is represented by Thread.NORM_PRIORTY. Priority level range between 1 to 10.

Priority level

Thread.MIN_PRIORITY -- Minimum priority of thread which is 1
Thread.NORM_PRIORITY -- Normal priority of thread which is 5
Thread.MAX_PRIORITY -- Maximum priority of thread which is 10

When new thread will execute?

  • When a thread in Runnable state either terminate or enter into block state.(waiting for I/O operation)

  • When a high priority thread enter into runnable state. Lower priority thread will be preemted and enter into waiting state.

Thread priority level can be changed

class th implements Runnable 
public void run()
int i;
public static void main(String arg[])
th ob=new th();
Thread a=new Thread(th);

System.out.println("Priority of a:"+a.getPriority());
System.out.println("Priority of a:"+a.getPriority());
Priority of a:5
Priority of a:7