In this article Java Thread Priority we give the information about In Java, default priority is set by JVM for every thread. When a thread is created, the thread priority is also set by the programmer.

Java Thread Priority:

In Java, default priority is set by JVM for every thread. When a thread is created, the priority is also set by the programmer. To set these priorities, there are two methods in Java: getPriority() and setPriority.

Java Thread Priority

Priorities range from 1 to 10.

Constants for Thread Priorities

public static int MIN_PRIORITY: This is the minimum priority for the thread. Its value is 1.

public static int NORM_PRIORITY: This is the default priority of the thread. This priority is set when no priority is given to the thread. Its value is 5.

public static int MAX_PRIORITY: This is the maximum priority. Its value is 10.

Rules for Thread Priority: 

  1. JVM provides the priority to each thread and according these priorities JVM allocate the processor.
  2. Priority are separated in the form of integer values which ranges from 1 to 10.
  3. MIN_PRIORITY: 1
  4. NORM_PRIORITY: 5
  5. MAX_PRIORITY: 10
  6. Priority are inherited from parent thread
  7. By default main thread priority is 5.
  8. if priority value is not between 1 to 10 then it will throw run time exception (IlligalArgumentException)
  9. Priority depends on the platform but windows not supported by priorities.

Example for Default Thread Priority

Source Code:

import java.lang.*;

public class ThreadPriEx extends Thread
{
public void run()
{
System.out.println(“Child Thread”);
System.out.println(Thread.currentThread().getPriority());
}
  public static void main(String [] args)
{
  System.out.println(“Parent Class”);
System.out.println(Thread.currentThread().getPriority());
ThreadPriEx t=new ThreadPriEx();
t.start();
}
}

Output :

Compile: javac ThreadPriEx.java

Run: java ThreadPriEx
Parent Class
5
Child Thread
5

Example for set Priority

At the same time all the threads are ready for execution, but when the priority is set then the thread which has maximum priority is ready for execution first.

Source Code:

import java.lang.*;
public class SetPriEx extends Thread
{
public void run()
{
System.out.println(“Child Thread”);
System.out.println(“Child Thread Priority: “+Thread.currentThread().getPriority());
}
public static void main(String [] args)
{
SetPriEx t=new SetPriEx();
t.run();
System.out.println(“Main Thread Old Priority: “+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(7);
System.out.println(“Main Thread new Priority: “+Thread.currentThread().getPriority());
t.run();
}
}

Output:

Compile: javac SetPriEx.java

Run: java SetPriEx
Child Thread
Child Thread Priority: 5
Main Thread Old Priority: 5
Main Thread new Priority: 7
Child Thread
Child Thread Priority: 7

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *