Java- Thread Priority

Thread Priority

Each thread has a priority. Priorities are represented by a number between 1 and

  1. In most cases, thread scheduler 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.

  2. public static int MIN_PRIORITY //1

  3. public static int NORM_PRIORITY //5 (default)

  4. public static int MAX_PRIORITY //10

public class ThreadPriority extends Thread{
	@Override
	public void run() {
	Thread th= Thread.currentThread();	
	System.out.println("Name :"+th.getName() +"\t Priortity:"+th.getPriority());
	}
	public static void main(String[] args) {
 ThreadPriority t1 = new ThreadPriority();
 ThreadPriority t2 = new ThreadPriority();
 ThreadPriority t3 = new ThreadPriority();
 
 t1.setPriority(MIN_PRIORITY);
 t2.setPriority(NORM_PRIORITY);
 t3.setPriority(MAX_PRIORITY);
 
 t1.start();
 t2.start();
 t3.start(); 
	}
}
----------------------------------
Name :Thread-2	 Priortity:10
Name :Thread-1	 Priortity:5
Name :Thread-0	 Priortity:1

Even though t1 starts first, it has MIN_PRIORITY so, it executes last that to depends on JVM Specification